I've never seriously read golang code in my life before and I'm so deeply confused right now

I'm literally *one stack frame deep* and I'm seeing code that makes me want to scream

no sensible language would allow this to work even when SERVER_PORT isn't set:

port := os.Getenv("SERVER_PORT")
lis, err := net.Listen("tcp", "localhost:"+port)

Show thread

> If the port in the address parameter is empty or "0", as in "" or "[::1]:0", a port number is automatically chosen. The Addr method of Listener can be used to discover the chosen port.


Show thread

@iliana okay at least that one makes sense (it's choosing an ephemeral port, the rules for which... could.... be different for non-tcp/udp protocols)

@iliana For bonus points, did you know that the `Addr` may not actually be what it's listening on?

That is to say, the following code can panic in some cases:

conn, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal("couldn't listen")
_, err = net.Dial("tcp", conn.Addr().String())
if err != nil {
panic("this does happen in some cases")

For anyone curious about the details, it's basically "ipv6"

In go's defence, the behaviour was fixed in go1.9, so it probably can't panic anymore (whoops, didn't realize that until I looked it up now)

@iliana this strikes me as the sort of thing that was necessary inside google and they never considered that it'd be a bad idea outside

@iliana "To distinguish between an empty value and an unset value, use LookupEnv" oh god what

LookupEnv(string) (string, bool)

... okay you should have just made GetEnv return that, assholes

@riking @iliana Oh, that's not particularly worse than python's stuff for accessing environment variables.

"When putenv() is supported, assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ."

@iliana It's a little like Java, without the C++ bullshit.

@iliana that feeling has not yet left me like a year in. i can read and write java, ruby, python, and even a bit of js. trying to follow code doing anything of reasonable complexity in go just breaks my brain somehow.

Sign in to participate in the conversation

Cybrespace is an instance of Mastodon, a social network based on open web protocols and free, open-source software. It is decentralized like e-mail.