go-basics
Go language basics
This section includes exercises and small projects which illustrate the basics of Go programming language. Things such as: types, data structures, functions, pointers, packages and others.
Section overview
- Go Learning Resources
- Go Program Anatomy
- Number Systems
- Basic Types
- Constants
- Packages
- Vendor directory
- Custom import paths
- Custom package manager
- Package Management
- Go modules
- Go module proxy
- Internal directory
Installation
Before trying any of these examples make sure to have the Go
binary installed on your platform.
OSX:
# Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install Go
brew install go
Linux:
# Move into $HOME directory
cd ~
# Download the binary for your distribution
curl -O https://dl.google.com/go/go1.12.linux-amd64.tar.gz
# Verify that the downloaded binary is not corrupted. Check if the hash matches the one from downloads page
sha256sum go1.12.linux-amd64.tar.gz
# Extract the binary
tar xvf go1.12.linux-amd64.tar.gz
# Make root user the owner of Go workspace
sudo chown -R root:root ./go
# Move go directory to a standard location
sudo mv go /usr/local
For more details regarding Go
installation check out
For downloading Go
binary for your platform checkout
To check if you installed Go
successfully type:
# Displays installed Go version
go version
# Displays all environment variables defined by Go
go env
Docker:
docker run -it golang:1.11
Set $GOPATH variable
sudo nano ~/.profile
# Linux
export GOPATH=/usr/local/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:/$GOBIN
# OSX
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:/$GOBIN
# Save File
# Restart shell configuration
source ~/.profile
Other Go tools
You can also install other Go
tools which will help you have a more productive development process
Go Tools
go get -u golang.org/x/tools/...
Go Lint
go get -u golang.org/x/lint/golint
Go Imports
go get -u golang.org/x/tools/cmd/goimports
Daily routine things
-
Format your entire code recursively using
go fmt ./...
-
Lint your entire code recursively using
golint ./...
For more info about Go check out
For more info about Go commands & WORKSPACE check out
β
FEEDBACK
π
COMMUNITY
Happy hacking gophers