Master Go Programming: The Complete Go Bootcamp
incomplete
Getting Started
GOPATH and Code Organization
Go requires you to organize your code in a specific way
Go programmers typically keep all their code in a single workspace
A workspace is nothing more than a directory in your file system whose path is stored in an environment variable called
GOPATH
On windows, it's in
%USERPROFILE%\go
On Macs & Linux, it's in
~go
You can print out the values of the environment variables by running the
go env
command
Go Workspace (GOPATH)
The work directory (GOTPATH
) contains the following 3 subdirectories at its root:
src
: contains the source files for your own or other downloaded packagespkg
: contains go package objects (aka package archives)these are non-executable files or shared libraries ending in
.a
bin
: contains compiled & executable Go programswhen you run
go install
on a program directory, Go will put the executable file under this file
To run/compile programs (CLI):
go run <filename.go>
Compiling (go build) & Running Go Applications (go run)
go is a tool for managing Go source code
go run
: compiles & runs the applicationit doesn't produce an executable
go run <filename.go>
compiles & immediately runs the Go programgo run -x <filename.go>
gives details about the compilation process
go build
: it just compiles the applicationproduces an executable
go build <filename.go>
compiles a bunch of Go source filescompiles packages and dependencies
go build
will compile the files in the current directory and will produce an executable file with the name of the current working directorygo build -o app
produces an executable file called app
compiling for windows:
GOOS=windows GOARCH=amd64 go build -o winapp.exe
compiling for linux:
GOOS=linux GOARC=amd64 go build -o linuxapp
compiling for mac:
GOOS=darwin GOARCH=amd64 go build -o macapp
go install
both
go install
&go build
will compile the package in the current directoryif the package is main,
go build
will place the resulting executable in the current directory andgo install
will move the executable toGOPATH/src
Formatting Go Source Code (gofmt)
Go strongly suggests certain styles
gofmt
which comes from golang formatter will format a program's source code in an idiomatic way that is easy to read and understandgofmt
is built into the language runtime and it formats Go code according to a set of stable, well-understood language rulescan be run manually at the command line or can configure our IDE to rub
gofmt
each time a file is saved
Example: gofmt -l -w main.go
-l
: tells you which file was modified-w
: if not formatted correctly, will overwrite fileif you run
gofmt
in the directory, all source files will be formatted correctly
Go Basics
Variables in Go
a variable is a name for a memory location where a value of a specific type is stored
a variable belongs and it's created at runtime
a declared variable MUST be used or get an error!
_
is the blank identifier and mutes the compile time error returned by unusual variables
Declaring variables:
using the
var
keyword
using the Short Declaration Operator (
:=
)