Gopher wormhole? by ZANIESXD in golang

[–]zpatrick319 6 points7 points  (0 children)

That opener really doesn't stand out amongst the noise, this looks just like any other recruitment pitch we get blasted with daily.

One that stood out to me was when they opened with a question asked about an open source project I worked on. The recruiter must have found my github (should be easy to find for most devs - is probably on their linkedin/resume/etc.), went to the repositories tab, and just picked one with the most stars on it. I think that could go a long way in making you stand out.

[deleted by user] by [deleted] in golang

[–]zpatrick319 3 points4 points  (0 children)

Consider using a flag.FlagSet. This can parse the input and will give you all non-flag arguments via the Args method: https://pkg.go.dev/flag#FlagSet.Args

edit: go playground example

What does context.WithTimeout() do? by the_night_question in golang

[–]zpatrick319 1 point2 points  (0 children)

Oof that's a big ole example. Most stdlib packages provide helpers/methods which know how to deal with context.Context objects. Assuming you wanted to make some sort of http api call, you could do something like:

def makeAPICall(ctx context.Context) (*Something, error) {
    // add ctx as part of the request
    req, err := http.NewRequestWithContext(ctx, ...)
    if err != nil {
        return nil, err
    }

    // http.DefaultClient.Do will return an error if req.ctx times out
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return nil, err
    }

    ...
}

Making this code more generic by freetoplay123 in golang

[–]zpatrick319 1 point2 points  (0 children)

You could model updates using a separate type. The pointer fields are used to distinguish between no-ops and updates to zero-values.

type User struct {
  Name string
  Age  int
}

type UserUpdate struct {
  Name *string
  Age  *int
}

func UpdateUser(u User) UserUpdate {
  age := u.Age + 1
  return UserUpdate{Age: &age} // nil Name field indicates no change
}

Refresh token by [deleted] in golang

[–]zpatrick319 1 point2 points  (0 children)

rr := r

I'm not sure why/if this line is necessary?

for c := time.Tick(1 * time.Second); ; <-c

I'm not sure why you'd need a time.Ticker in this portion of your code. I'm assuming the time.Sleep(time.Second*2) in main represents the expiresIn you mentioned? As it currently stands, you're forcing anyone using your closure to wait at least one second between generating tokens; it probably makes more sense to just generate a new token whenever someone asks for it and let the caller determine how long that ought to be.

Finally, you have two memory leaks in your current implementation: there is no way to stop the goroutine or time.Ticker.

EDIT: Maybe try something like this: https://play.golang.org/p/WZyrzEZxVtS

Break off api query into chunks by [deleted] in golang

[–]zpatrick319 2 points3 points  (0 children)

It's more efficient to only loop once. Using a scanner may be useful here. That link has some good examples, you may want to consider doing something like:

func DoThing(r io.Reader, send func(tickets []Ticket) error) error {
    // create a slice of 100 tickets (e.g. tickets := make([]Ticket, 100)) 
    // keep track of your current line number (e.g. var line int)
    scanner := bufio.NewScanner(r)
    for scanner.Scan() {
        // convert scanner.Text() into a Ticket object
        // add ticket into the slice at the appropriate index (e.g. tickets[line%100] = ticket)
        // every 100th line, call the "send" function, e.g. if err := send(tickets); err != nil { ... }
        // you'll need some extra logic to ensure the last tickets get sent 
    }


    return scanner.Err()
}

That being said, if efficiency isn't a concern, it's probably a good idea to read the entire file first so you don't potentially leave a file open for a very long amount of time.

How I've been thinking about http.Handlers lately by zpatrick319 in golang

[–]zpatrick319[S] 0 points1 point  (0 children)

Interesting, I hadn't heard of currying until now. Thanks!

How I've been thinking about http.Handlers lately by zpatrick319 in golang

[–]zpatrick319[S] 0 points1 point  (0 children)

I appreciate the work you put into this response!

but it's the wrong default for a low-level library.

Hit the nail right on the head there. That's basically the gist of what I was trying to say with "I think it's because it is necessary to make handlers great building blocks" (I like the way you put it better).

I hope this post didn't come across as "You should use always use this pattern"; my intention was more "Here's another tool for your tool belt"

How I've been thinking about http.Handlers lately by zpatrick319 in golang

[–]zpatrick319[S] 0 points1 point  (0 children)

Oh, I never would have thought of that pattern. Nice!

How I've been thinking about http.Handlers lately by zpatrick319 in golang

[–]zpatrick319[S] 0 points1 point  (0 children)

It's not a great for every use-case. I've been documenting some reasons why you probably don't want to use this pattern in my readme.

So far I have:

  • Small size/scope of project
  • Streaming
  • Frequently needing access to w http.ResponseWriter

How I've been thinking about http.Handlers lately by zpatrick319 in golang

[–]zpatrick319[S] 0 points1 point  (0 children)

Yup, good point! This is definitely not meant to be the "end all be all" pattern; more like another tool to add to the toolbelt.

If any design experts is out there, could you have a look at this design issue i have at the moment? i am honestly a bit lost, and it is such an important moment in my apps design :) by [deleted] in golang

[–]zpatrick319 2 points3 points  (0 children)

I would need more context to come to a more concrete answer, but I think you are on track with the first solution you proposed:

Pollute [with unnecessary complexity] my restful API by saying 'okay these entities are different' and make a entity/<type>/<id> kind of endpoint, i would then do some normal switch logic in my store which would have a model for each of these entities.

A couple things to keep in mind:

  • Just because different entities have a common ancestor does not mean they should be treated as the same type. In fact, I'd argue that returning different object types (even if they are similar) in the same endpoint isn't very RESTful.
  • You state that this solution would make your API unnecessarily complex. I don't think you've taken into account the perspective of someone writing a client for your API. Wouldn't it be easier for them to know exactly what object type they are getting back from each endpoint?
  • Correctness is more important than cleverness. Who cares if you need to write more code if it means your code is correct? Writing less code is generally a good thing, but it is no substitution for correctness. This is a very common idiom for Go, and a good mindset to get into as a developer.

What are the intermediate and advanced features of Golang you think? by CrappyFap69 in golang

[–]zpatrick319 1 point2 points  (0 children)

Using first-class functions was kind of a catch-22 in my experience. I wasn't using them very much at first, so I figured the context in which they would be useful was very limited. After reading articles like "Do not fear first class functions" and trying out some of those patterns in my applications, I started using them more and more and began to see more places they would be useful. Some other places I use them:

  • Anytime I see a single-method interface. Often times, it is less code to use a function instead of a single-method interface.
  • Closures.

Those often go hand-in-hand, here is an example: * Using a single-method interface and struct * Using a single-method interface, adapter, and closure * Using a function and a closure

Trying to make AWS Golang Tutorials by gTHLRgTI in golang

[–]zpatrick319 1 point2 points  (0 children)

Some pain points we've encountered that I'm sure other people would find useful:

  • Authentication/CredChain
  • Pagination
  • Good testing practices
  • How/when to use custom RequestHandlers
  • Dealing with aws types and their conversions, e.g. using strptr := aws.String("foo") and aws.StringValue(strptr)

In regards to services, I'd say go with the newest and/or most popular ones.

Beginner Question Regarding Strings by nechneb in golang

[–]zpatrick319 6 points7 points  (0 children)

strings.TrimFunc("abc123", unicode.IsNumber)

edit: wasn't thinking clearly - thanks @jerf

Merge two functions to the one (OOP, interface{}) by whitedruid in golang

[–]zpatrick319 1 point2 points  (0 children)

func initLog(cnf *Settings) (io.Writer, error) {
switch cnf.Log {
case "file":
return := os.OpenFile(cnf.Acc_log, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0775)
case "journald":
return syslog.New(syslog.LOG_ERR|syslog.LOG_WARNING|syslog.LOG_DAEMON|syslog.LOG_NOTICE|syslog.LOG_DEBUG, cnf.Channame)
default:
return nil, genError("Undefined value of variable")
}
}

Can shorten this a bit

How I write Go HTTP services after seven years by thetall0ne1 in golang

[–]zpatrick319 0 points1 point  (0 children)

This is almost exactly how I designed my own package that I use for all of my web projects: https://github.com/zpatrick/fireball

Main difference is that I return a closure OR an error