Learning Go (Day 1.1)

I just sat down to continue my education in Go, but then I had more thoughts! So, let me write these down before I forget:

No Keyword Arguments

Keyword arguments have been one of my favorite language features ever since I was first introduced to them in... Python? Ruby? One of those. Sure, they make my code a little more verbose, and they might introduce a little additional overhead when invoking functions. Still, they make the code significantly more readable and maintainable, which I find to be more important in the majority of cases.

Unfortunately, it looks like Go only supports positional arguments. You can sorta work around this by encapsulating the arguments in a struct, but that has its own problems. Wow -- what a perfect segway into my next gripe!

No Optional Parameters

Function parameters in Go do not support default values. Some languages such as Java work around this limitation by allowing you to overload function definitions. Go... does not. 😔

Once again, you can sorta work around this via the struct initialization syntax. For example:

type person struct {
	first string
	last  string
	age   int
}

func main() {
	// The `age` parameter is omitted, so it will be initialized
	// with its "zero value" (Erm... which is `0` in this case)
	var p = person{
		first: "Brian",
		last:  "Lauber",
	}

	// Output shows that `age` is `0`
	fmt.Println(p)
}

The problem is that it's often important to distinguish between a parameter that was omitted versus a parameter that was intentionally set to its "zero value". For example: when you order coffee, you might tell the cashier that you want "no sugar". You'd be a little upset if they then proceeded to add 3 packets of sugar because that was the "default amount"! 😵‍💫

Currently, I'm not aware of any good way to work around this limitation in Go. Perhaps it's just something I'll need to get used to?

Meta-programming?

So far, I haven't seen any mention of meta-programming in Go. If the capability exists, then it might provide a way to work around the limitations mentioned above. Then again, such a solution might just be complicating things by fighting against the language. Gah!

In Conclusion...

Honestly, I'm still interested in learning more about Go! I just appreciate languages that are very expressive and provide a lot of flexibility.

With that being said, it's time to continue learning Go! 💪

Previous Post

Learning Go (Day 1)