this post was submitted on 25 Sep 2023
4 points (62.5% liked)

Golang

2220 readers
1 users here now

This is a community dedicated to the go programming language.

Useful Links:

Rules:

founded 1 year ago
MODERATORS
 

Did I break a taboo by doing this?

all 10 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 1 year ago (1 children)

I don’t think it is a taboo, but it is possibly just worse code in many cases. Handling error values is usually something that should be done thoughtfully. Panic is nice but can easily be overused and should not make it out of a package.

I too wish there was a better way, but in the form of proper enums and result and option types. However, wrapping in functions is fine in some cases not taboo.

As an aside, you probably don’t need a static union type for your min and max functions. I assume you could use comparable.

[–] [email protected] 2 points 1 year ago (1 children)

As an aside, you probably don’t need a static union type for your min and max functions. I assume you could use comparable.

comparible only allows ==

[–] [email protected] 2 points 1 year ago (1 children)

Oh right! What am I saying? contraints.Ordered maybe.

[–] [email protected] 1 points 1 year ago

I just looked at the standard docs and I didn't realize there was a built-in min and max function. Y'all should really read the standard lib docs.. it's fascinating in there.

[–] [email protected] 3 points 1 year ago (1 children)

I would rename Check to Must which there is at least some precedent for.

[–] [email protected] 1 points 1 year ago* (last edited 1 year ago)

Oh yeah and it's also less to type. Good idea!

Edit: here's the change https://github.com/danhab99/idk/blob/main/idk.go#L13

[–] [email protected] 3 points 1 year ago (1 children)

There is! See “Errors are values” from the Go blog: https://go.dev/blog/errors-are-values

[–] [email protected] 0 points 1 year ago

Yeah I know about that article.... Then again... I'm lazy

[–] [email protected] -4 points 1 year ago

I too consider that if err != nil a bit complicated to type. Most times, I wrap it away in a function like your Check0. I know that the major "framework" for command line applications (cobra) has a similar logic with its checkError function.