Go Basics
Let's study Go
A huge thank you to Calvin!
What is Go Language?
Programming language developed by Google
Syntax is similar to C language, but designed to handle Concurrency programming easily
Concurrency programmingMeans that the program can do other things first while performing relatively time-consuming operations such as DB requests or network communication like web services
Although it's a relatively recently emerged programming language, it's not complex and a practical language
Representative projects that utilize Go include
DockerandKubernetes
Why Go?
Go aims for a simple, concise, and intuitive language
Supports
OOPNo concepts of class, objects, inheritance
Keywords used in Go are only 25, half the level of Java
Simple!
Although it's a compiled language, the compiler speed is very fast, so it can be used like an interpreter language
Has a good build system
Build speed is very fast
Projects consisting of thousands of lines of code compile in just a few seconds!
Code importis based on off url importSo it's good for managing dependencies!
Go module allows you to hash URLs when importing
Similar to importing .lock files in other build systems!
Install Go on Ubuntu/Mac
1. Install Go 1.14
Ubuntu
Install using
longsleep/golang-backportsPPA
MacOS
Download & install package from golang.org
2. Check Go version
Ubuntu
MacOS
3. Workspace setup
Create a directory to proceed with Go projects
ex)
/Users/chloe/workspace/go
Create 3 directories inside that directory as follows
bin
Where OS-specific executable binary files are stored after source code is compiled
pkg
Where library files are stored after packages needed for the project are compiled
src
Where written source code and open source codes are stored
4. Environment variable setup
Go has 2 environment variables -
GOPATH&GOROOTGOROOTWhere Go binaries are located
If you install with
sudo apt install golang-go, Go is installed in the default path, so no need to modify separately!On Mac, it's installed in
/usr/local/go
GOPATHUser go environment path
Need to set where project files are located
When importing packages with
go import, it's loaded fromGOPATHGenerally set
GOPATHto$HOME/goImportant because when doing
go installorgo import, go looks forGOPATH!
4-1. Ubuntu $GOPATH setup
GOPATH setup
PATH setup
4-2. MacOS $GOPATH setup
Add the following to
~/.zshrc
+
After setting $GOPATH, you can use the go env command to check if it's properly applied!
5. src in $GOPATH
src in $GOPATHGo downloads and stores downloaded codes in
$GOPATH/srcclassified by downloaded domainex) github.com, golang.org, google-golang.org, etc.
For source code I write, I'll create a directory with github user name inside github.com
Getting started with Go
1. Main package
Naming the package main.go means you will compile this project and use it
That is, all packages except main.go are not compiled!
Since main is the entry point, the compiler looks for packages named main first
ex)
package main
Go looks for a function called func main() {}
This is the entrypoint of the Go program!
Automatically, the compiler first finds and executes the main package and the main function inside it
ex)
main.go
2. Packages and imports
To export a function in Go, write it in upper-case
ex)
something.go
main.go
Console output functions - println, print
println, printLet's output
ex)
print / println can be used without importing
fmtpackageprintln and print differ in the presence of line break
To make a line break with print, use
\n!
Single quotes don't work!
Use double quotes
print functions can output the results of operations inside functions!
ex)
fmt package
fmt packagePackage for input/output
How to use
Write
import "fmt"below package main
+
Useful Go resources recommended by Calvini
gobyexample.com
tour.golang.org
blog.golang.org
dave.cheney.net
https://golang.org/doc/effective_go.html
must read!
Last updated