Skip to content

librity/learning_go

Repository files navigation

Learning GO!

All my GO tutorials

Setup

asdf package manager install

Install the latest version of Go with the asdf version manager:

$ cd
$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf
$ cd ~/.asdf
$ git checkout "$(git describe --abbrev=0 --tags)"

# For Ubuntu or other linux distros
$ echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
$ echo '. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc
$ source ~/.bashrc

$ asdf plugin-add golang https://github.com/kennyp/asdf-golang.git
$ asdf install golang latest
$ asdf list
$ asdf global golang 1.17.1
$ asdf current
$ go help

VSCode

Install the official Golang extension:

Changing default$GOPATH

$ echo "export GOPATH=$HOME/code/go" >> ~/.bashrc
$ source ~/.bashrc

$ go env -w GOPATH=$HOME/code/go

Using external packages

$ go mod init
$ go get github.com/fatih/color
$ asdf reshim golang

Web Sockets Chat

Go the project's folder and execute all go files:

$ cd web_sockets_chat
$ go run ./*.go

Then open one of the demos in your web browser:

Notes

If it starts with an upper case it's public, else it's private. Genius!

Methods are implemented with a reciever declaration:

// Method copies reciever
func (receiver struct_type) Method(arg int) {
	receiver.value *= arg
}

// Method manipulates a pointer receiver
func (receiver *struct_type) Method(arg int) {
	receiver.value *= arg
}

Docs

Libs

Resources