On my recent job search I've come across quite a few postings with Go as their primary programming language. I believe that I can learn it on the job but I would rather give potential employers more confidence when it comes to my knowledge of Go. Also, I've always been interested in Go but never required to write it.
These are some of my jots of note when it comes to learning Go. This list is only beginning. I'll add here over time as I go along.
-
Immediately useful commands of note are
go mod init,go run,go help, andgo mod tidy -
Help documentation for any command is available with
go help - You can search for Go modules at https://pkg.go.dev
-
Go discovers module requirements depending on usage in source code when you run
go mod tidy -
In addition to fetching required modules and updating go.mod, the
mod tidycommand will also remove any unused modules from go.mod -
Alternatively, you can use
go getto fetch a particular module, but this should be used primarily for upgrading, downgrading, or removing existing modules -
If you do hand-modify your go.mod file, you can use
go mod edit -fmtto ensure it has the proper formatting - Go is opinionated when it comes to formatting, so there are specific conventions to follow
-
By convention, "Exported names" are those beginning with a capital letter, so a function named
Hellois exported (i.e. callable by external packages) while a function namedhellois not -
When the
:=operator is used to declare and initialize a variable, its type is infered from the value on the right-hand side -
Go distinguishes a package as a "main package" if it contains a Go source file with
package mainat the start of the file -
You can use
go mod editto add areplacein yourgo.modto point dependencies to packages on the local filesystem (this action should usually be followed bygo mod tidy) - You can use Go's Multiple return values feature to implement error handling
-
The standard library's
log.Fatal"is equivalent to Print followed by a call toos.Exit(1)" (see https://pkg.go.dev/log#Fatal)
The site https://pkg.go.dev has no API to search packages, but you can still achieve command-line package search by scraping the results from the search page.
Along with curl and tools from html-xml-utils, you can look up packages on the Go packages website:
read -p 'Search go packages: ' query && curl --get --data-urlencode "q=$query" "https://pkg.go.dev/search" | hxnormalize -x | hxselect -c -s '\n' '.SearchSnippet-synopsis, .SearchSnippet-header-path'
Alternatively, you can add this bash function to your profile and invoke it like gopkg <query>:
function gopkg {
if [[ $# -ne 1 ]]; then
echo "Usage: gopkg <query>"
return 1
fi
curl --get --data-urlencode "q=$1" "https://pkg.go.dev/search" | hxnormalize -x | hxselect -c -s '\n' '.SearchSnippet-synopsis, .SearchSnippet-header-path'
}