{"id":570,"date":"2022-04-23T18:42:29","date_gmt":"2022-04-23T18:42:29","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=570"},"modified":"2022-04-27T09:53:19","modified_gmt":"2022-04-27T09:53:19","slug":"learning-go-day-6","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/study-notes\/learning-go-day-6\/","title":{"rendered":"Learning Go – Day 6"},"content":{"rendered":"\n
Featured image by Egon Elbre<\/a><\/p>\n\n\n\n Installing Go (1.18) using the download from https:\/\/go.dev\/<\/a> on macOS Monterey, the GOPATH environment variable is not set by default. However reading the help for Executables are installed in the directory named by the GOBIN environment\nvariable, which defaults to $GOPATH\/bin or $HOME\/go\/bin if the GOPATH\nenvironment variable is not set.<\/p>\n<\/blockquote>\n NOTE:<\/strong> At this point in time you might get some conflicting or out of date information regarding how to start a go project and or to do with paths. From what I can tell is that at some point they added modules to go and also allowed you to have your modules\/code outside of the GOPATH.<\/p>\n I have seen a number of people use Cobra<\/a> and Viper<\/a> from spf13<\/a> (which rang a bell with me from when I used Hugo<\/a> in 2016, nice one I see he now works for Google).<\/p>\n However the standard library includes the Example of parsing a string flag named \u201cflagName\u201d.<\/p>\n Usage would be:<\/p>\n Recall that you can set the environment variable You can also set the CPU architecture using the environment variable For example: I want to test out this theory of cross compiling a binary to be run on another system. In fact the whole reason I got into learning Go is because I have some applications I want to run on my own Linux server.<\/p>\n The test is to cross compile a binary from my M1 Mac (arm64) and deploy it to my Ubuntu Linux running on Intel (amd64).<\/p>\n\n
\n
Lets continue on our journey<\/h3>\n
go help install<\/code> they state the following.<\/p>\n
\n
$ ls ~\/go\nbin\/ pkg\/\n<\/code><\/pre>\n
\n
$ go install github.com\/spf13\/cobra-cli@latest\ngo: downloading github.com\/spf13\/cobra-cli v1.3.0\ngo: downloading github.com\/spf13\/cobra v1.3.0\n...\n<\/code><\/pre>\n
\n
$GOPATH\/bin<\/code> or
$HOME\/go\/bin<\/code> as stated by the documentation.<\/li>\n<\/ul>\n
$ ls -la ~\/go\/bin\n...\n-rwxr-xr-x 1 andre staff 7.9M 27 Apr 10:40 cobra-cli*\n<\/code><\/pre>\n
\n
$GOPATH\/pkg\/mod<\/code> or
$HOME\/go\/pkg\/mod<\/code>.<\/li>\n<\/ul>\n
$ ls ~\/go\/pkg\/mod\ncache\/ github.com\/ golang.org\/ gopkg.in\/ honnef.co\/ mvdan.cc\/\n\n$ ls ~\/go\/pkg\/mod\/github.com\nfsnotify\/ hashicorp\/ magiconair\/ mitchellh\/ pelletier\/ spf13\/ subosito\/ yuin\/\n<\/code><\/pre>\n
\n
go get<\/code>. You run this from the directory in which your
go.mod<\/code> file lives.<\/li>\n<\/ul>\n
$ go get -u github.com\/spf13\/cobra@latest\ngo: downloading github.com\/spf13\/cobra v1.4.0\ngo: downloading github.com\/inconshreveable\/mousetrap v1.0.0\n...\n\n$ cat go.mod\nmodule learn\n\ngo 1.18\n\nrequire (\n\tgithub.com\/inconshreveable\/mousetrap v1.0.0 \/\/ indirect\n\tgithub.com\/spf13\/cobra v1.4.0 \/\/ indirect\n\tgithub.com\/spf13\/pflag v1.0.5 \/\/ indirect\n)\n<\/code><\/pre>\n
\n
gofmt<\/code>. I hear people on YouTube pronounce it \u201cgo fomt\u201d and same for the fmt package being pronounced \u201cfomt\u201d.\nVisual Studio Code with the Go extension automatically does the formatting on save.<\/li>\n<\/ul>\n
$ gofmt --help\nusage: gofmt [flags] [path ...]\n -cpuprofile string\n \twrite cpu profile to this file\n -d\tdisplay diffs instead of rewriting files\n -e\treport all errors (not just the first 10 on different lines)\n -l\tlist files whose formatting differs from gofmt's\n -r string\n \trewrite rule (e.g., 'a[b:len(a)] -> a[b:]')\n -s\tsimplify code\n -w\twrite result to (source) file instead of stdout\n\n# To format a file in place\n$ gofmt -s -w \/path\/to\/file.go\n<\/code><\/pre>\n
Documentation right at your fingertips<\/h3>\n
\n
$ go doc PACKAGE\n\n$ go doc flag\n...\n\n# See the documentation for flag.StringVar function\n$ go doc flag.StringVar\npackage flag \/\/ import "flag"\n\nfunc StringVar(p *string, name string, value string, usage string)\n StringVar defines a string flag with specified name, default value, and\n usage string. The argument p points to a string variable in which to store\n the value of the flag.You can also use [godoc](http:\/\/golang.org\/x\/tools\/cmd\/godoc) to serve the documentation from your local machine.\n<\/code><\/pre>\n
\n
Parsing CLI flags<\/h3>\n
flag<\/code> package that suits most basic needs.<\/p>\n
import "flag"\n\nvar name string\nflag.StringVar(&name, "flagName", "default value", "help description"\nflag.Parser()\n<\/code><\/pre>\n
$ go run . --help\n...\n -flagName string\n help description (default "default value")\n\n$ go run . -flagName Awesome\n<\/code><\/pre>\n
\n
panic<\/code> to indicate something went bust.<\/li>\n
strings<\/code> from the standard library.<\/li>\n<\/ul>\n
Cross compiling<\/h3>\n
GOOS<\/code> to \u201clinux\u201d, \u201cdarwin\u201d or \u201cwindows\u201d for the operating system before issuing
go build<\/code>.<\/p>\n
GOARCH<\/code>.<\/p>\n
GOOS=linux GOARCH=arm64 go build -o linux-arm64<\/code><\/p>\n
Seeing is believing<\/h3>\n