Go Programming Update: Day 2 – The Weird, the Wonderful, and the “Why Though?”
Hello again, and thank you for taking a moment to read through.
I understand your time is valuable, so I’ll keep this brief going forward.
🧠Yesterday Was... Interesting.
Yesterday was another opportunity to experience something brilliantly human: a programming language created by man — Go.
Now, here are a few things I’ve learned that made me pause and go, “Wait, what?”
"In Go, it's common to write methods that gracefully handle being called with a nil receiver."
But… look at this code:
package main
import "fmt"
type I interface {
M()
}
type T struct {
S string
}
func (t *T) M() {
if t == nil {
fmt.Println("<nil>")
return
}
fmt.Println(t.S)
}
func main() {
var i I
var t *T
t.S = ""
i = t
describe(i)
i.M()
i = &T{"hello"}
describe(i)
i.M()
}
func describe(i I) {
fmt.Printf("(%v, %T)\n", i, i)
}
Output:
panic: runtime error: invalid memory address or nil pointer dereference
There's nothing “graceful” about that! 😅
But honestly, if I had seen this code a week ago, I could only understand maybe 30% of it. Go code feels more like advanced programming—almost in the league of low-level languages.
🧱 Constructors and NewSomething
Then I stumbled on this: image.NewRGBA
.
That threw me off completely at first. Coming from PHP or JavaScript, I assumed New
was part of the method name.
Turns out, New is a convention, not a keyword. The actual type is image.RGBA
. Go uses NewTypeName()
to return an initialized instance — like a constructor function.
🟰 nil
Instead of null
Go doesn’t use null
— it uses nil
.
🚫 The Underscore _
Is Not Just a Placeholder
for _, v := range arr {
// Do something with v only
}
_, ok := someFunc()
The underscore isn’t decorative — it literally means: “Ignore this.”
💡 Keep an Open Mind
If this is your first time learning Go, please… don’t get your hopes too high in the first few days.
You're going to say “Hmm, this is weird” more than once.
- No shorthand
if
statements — braces{}
are required. - No
class
keyword — methods are tied to types via receivers. - No inheritance — Go favors composition.
- Go forces unused imports and variables to be removed — and that's a good thing.
🧬 Go Method Syntax
func (receiver Type) MethodName(arg ArgType) ReturnType {
// ...
}
If you don’t have a receiver, it's just a function. With a receiver, it becomes a method.
🔠Naming Conventions
Go uses PascalCase like this: ErrNegativeSqrt
.
I’m used to camelCase
and snake_case
, so this is another learning curve.
🧠Old Habits Die Hard
I still find myself:
- Using
()
inif
statements - Ending lines with
;
- Trying to destructure like it’s JavaScript 😅
But that’s okay — it’s part of the learning curve.
🎉 I Completed the Tour of Go!
In just 2 days, I finished the official Go Tour tutorials! 🎉
I’m proud of this progress. It’s a strong start, and I’m beginning to appreciate the beauty and simplicity Go aims for — even if it’s not always obvious at first.
💙 Onward and Deeper
Next, I’ll dive into more advanced Go concepts: goroutines, channels, error handling, map, struct, slice and possibly building an API.
Until my next update, stay safe and stay healthy.
No comments:
Post a Comment