{"id":547,"date":"2022-04-16T09:15:00","date_gmt":"2022-04-16T09:15:00","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=547"},"modified":"2022-04-22T10:21:00","modified_gmt":"2022-04-22T10:21:00","slug":"learning-go-day-1","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/study-notes\/learning-go-day-1\/","title":{"rendered":"Learning Go – Day 1"},"content":{"rendered":"\n
Featured image by Egon Elbre<\/a><\/p>\n\n\n\n I decided that it was finally time to sit down and learn me some Golang<\/a>.<\/p>\n Please note that this post and the following posts in my \u201cseries\u201d of Learning Go is meant to be notes to my future self and not meant to be a tutorial series for anyone else reading this. Therefore there will be lots of prebaked-in-assumptions and context that any other reader might not have. Again these are notes to myself.<\/p>\n Ok so at this point I decided Go looks amazing and that it is worth my time learning it.<\/p>\n Any variable or function which starts with a capital letter are exported names in go. Only exported functions and variables can be accessed from other packages.<\/p>\nBefore Day 1 there was Day 0<\/h3>\n
\n
\n
Day 1<\/h3>\n
\n
\n
$ mkdir -p ~\/Learn\/Go\/golangbot-series\n$ cd ~\/Learn\/Go\/golangbot-series\n<\/code><\/pre>\n
Very basics<\/h3>\n
\n
go mod init<\/code>. This will create a go.mod file in the current directory.<\/li>\n
go run .<\/code> to run the current module. It looks for a .go file that is part of the
main<\/code> package and runs the
main<\/code> function. NOTE: It does not leave build artifacts in the current directory.<\/li>\n
--work<\/code> with
go run<\/code> to see which temporary directory is used to build and run from.<\/li>\n<\/ul>\n
$ go run --work .\nWORK=\/var\/folders\/qv\/nt2bk76d50d2j53w12thd0200000gn\/T\/go-build2629236756\nHello world!\n<\/code><\/pre>\n
\n
go build<\/code> to build an executable. By default it is placed in the current directory.<\/li>\n
$ GOOS=linux go build\n$ GOOS=windows go build\n<\/code><\/pre>\n
Variable declaration<\/h3>\n
\/\/ var name type\nvar age int\nvar age int = 5\nvar age = 5\n\/\/ short hand declaration\nage := 5\n\n\/\/ declare one or more of same type\n\/\/ note: this will be initialized to 0\nvar x, y int\n\nx, y, name := -2, 42, "Jannie"\n<\/code><\/pre>\n
\n
var (\n\t\tname = "Andre"\n\t\tage = 5\n\t)\n<\/code><\/pre>\n
\n
bool \/\/ true, false\nbyte \/\/ alias for uint8\nint, int8, int16, int32, int64\nuint, uint8, uint16, uint32, uint64\nfloat32, float64\ncomplex64, complex128\nrune \/\/ alias for int32 and is used as Unicode code point\nstring\n<\/code><\/pre>\n
\n
import "unsafe"\nfmt.Printf("type of a is %T, size of a is %d\\n", a, unsafe.Sizeof(a))\n<\/code><\/pre>\n
\n
const a = 5\nconst (\n\tage = 5\n\tname = "Jannie"\n)\n<\/code><\/pre>\n
\n
Functions<\/h3>\n
func name(param1 type, param2 type) returnType { \n...\n}\n\nfunc name(param1, param2 sameType) ...\n<\/code><\/pre>\n
\n
func something(timesTwo, timesThree int) (int, int) {\n\treturn timesTwo * 2, timesThree * 3\n}\n\nmultTwo, multThree := something(2, 2)\nfmt.Println(multTwo, multThree)\n<\/code><\/pre>\n
\n
func rectProps(length, width float64) (area, perimeter float64) { \n area = length * width\n perimeter = (length + width) * 2\n return \/\/no explicit return value\n}\n<\/code><\/pre>\n
\n
_<\/code><\/li>\n<\/ul>\n
_, mult3 := somefunction()\n<\/code><\/pre>\n
Packages<\/h3>\n
\n
main<\/code> package and main function.<\/li>\n
$ go mod init nameOfModule\n<\/code><\/pre>\n
\n
\n
\n
import (\n\t_ "somemodule\/package"\n)\n<\/code><\/pre>\n
Control flow<\/h2>\n