A Tour of Golang - First learning Go Programming Language
The Go Playground - Available for share
Follow the instructions for your platform to install the Go tools: https://golang.org/doc/install#install. It is recommended to use the default installation settings.
On Mac OS X and Linux, by default Go is installed to directory /usr/local/go/
, and the GOROOT environment variable is set to /usr/local/go/bin
.
Your Go working directory (GOPATH) is where you store your Go code. It can be any path you choose but must be separate from your Go installation directory (GOROOT).
The following instructions describe one way you can set your GOPATH. Refer to the official Go documentation for more details: https://golang.org/doc/code.html.
Mac OS X and Linux
Set the GOPATH
environment variable for your workspace:
export GOPATH=$HOME/go
Also set the GOPATH/bin
variable, which is used to run compiled Go programs.
export PATH=$PATH:$GOPATH/bin
go run [path/to/file].go
This command only runs one file with main() function declared. It's not working with other files in the same package.
go build
This command build all files in package main into build file tour-golang, which is executable, you can run
./tour-golang
go run *.go
This command is similar with build package and run the executable file. It will link files .go
and run them.
In a directory, one package is declared at the same level. If we want to create another package, create sub-folder and declare with new package for subfolder.
Name of package should be name of directory.
Golang uses first character with capitalized (from A-Z
) is exported, that means other package can read.
Example in directory packages
package A
// Foo : exported function, should be comment by go-lint
func Foo() {}
func bar() {
// this is private in package A, but other file in package A can use bar()
}
// Animal : type animal is exported, shoul be comment by go-lint
type Animal struct {
a string // that is private
B int // that is exported, that mean public
}
And in package B
package B
import "github.com/huynhsamha/tour-golang/packages/A"
func todo() {
A.Foo() // OK
// A.bar()
// cannot refer to unexported name A.bar
// x := A.Animal{"123", 4}
// implicit assignment of unexported field 'a' in A.Animal literal
x := A.Animal{B: 123}
// x.a = "123"
// x.a undefined (cannot refer to unexported field or method a)
x.B = 123
}
- Variable - Function declaration
- For Statement
- If Statement
- Switch Statement
- Array
- Map
- Map with interface
- Function as values
- Function as closures
- Interface
- Struct
- Example Fibonaci
- Example Power
- Spread (...) - Unpacking array to arguments
- Same type in struct declaration
- Exported modules in package
- Sortings
- Error return
- Read / Write files
- JWT - jwt-go Golang JSON Web Token
- Crypto (SHA256, MD5, ...)
- Random Numbers
- Random Hex, String,... Simple
- Go Routine
- Go by Examples: https://gobyexample.com/
- Go Bootcamp: http://www.golangbootcamp.com/book