Intro
I started poking at Golang last year, but since I never needed it at work, I forgot everything pretty quickly. Also wasn’t a big fan of Golang’s package management, so I shelved it and switched to Rust — wrote a few small demos. Then for various reasons I picked Golang back up and started learning again. True to the “talk is cheap” principle, I built a small toy project. I think it works well enough as a tutorial (for people who already have some programming experience). It also just goes to show that a language is really just a tool. What actually matters is algorithms, design patterns, and programming thinking. I’m not going to compare it against other languages here — just pick the right (or good enough) tool for the job.
1. Basics
Start by running through Golang tour. Golang doesn’t have classes, but that doesn’t stop you from using struct methods as a rough equivalent — if I recall correctly, Rust does the same thing. Here’s a quick demo covering the basic syntax:
|
2. Advanced
The “slightly more advanced” stuff includes pointers, references, Map, interfaces, Slices, goroutines, and callbacks. You’ll want to go through the Golang tour or a book to learn these systematically — they’re considered the more advanced usage. Below are some beginner-level examples of advanced features, taken from my Golang learning project boomb on GitHub.
|
Basic struct definition.
|
package main |
As you can see, Fire takes a function as a parameter and calls it via a goroutine, storing the results in a channel. If you can follow the code above, you’ve got a solid grasp of channels, callbacks, pointers, and structs.
3. Package Management with dep
Put your project under $GOPATH, typically ~/go/src/, then run dep init inside the project directory. After that, dep takes over package management. You’ll see a new vendor directory along with Gopkg.lock and Gopkg.toml files. Run dep ensure to download dependencies locally into the current project. Add -v to any of these commands for more verbose output: dep ensure -v. To add a new dependency, use dep ensure add xxxxx.
Gopkg.toml is dep’s config file:
|
Packages listed in required will always be pulled into vendor during ensure. Just make sure you don’t push the vendor directory when committing to version control.
4. Misc
- Use a Makefile to manage project commands
- Add
-ldflags="-s -w"duringgo buildto reduce binary size https_proxy=127.0.0.1:1080 go get xxxxcan solve some package installation issues- A good IDE saves a ton of headaches