Let's study Go
A huge thank you to Calvin!
- Programming language developed by Google
- Syntax is similar to C language, but designed to handle Concurrency programming easily
Concurrency programming- Means 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
-
Go aims for a simple, concise, and intuitive language
- Supports
OOP- No 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
- Supports
-
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 import- So it's good for managing dependencies!
- Go module allows you to hash URLs when importing
- Similar to importing .lock files in other build systems!
- Go module allows you to hash URLs when importing
- So it's good for managing dependencies!
- Build speed is very fast
-
Install using
longsleep/golang-backportsPPAsudo add-apt-repository ppa:longsleep/golang-backports sudo apt update sudo apt install golang-go
- Download & install package from golang.org
chloe@chloe-XPS-15-9570 ~
$ go version
go version go1.14.2 linux/amd64~ via ⬢ v14.15.1
➜ go version
go version go1.15.6 darwin/amd64- Create a directory to proceed with Go projects
- ex)
/Users/chloe/workspace/go
- ex)
- 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
- bin
- Go has 2 environment variables -
GOPATH&GOROOT GOROOT- Where 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
- On Mac, it's installed in
GOPATH- User go environment path
- Need to set where project files are located
- When importing packages with
go import, it's loaded fromGOPATH - Generally set
GOPATHto$HOME/go- Important because when doing
go installorgo import, go looks forGOPATH!
- Important because when doing
GOPATH setup
export GOPATH=$HOME/goPATH setup
export PATH=$PATH:$GOPATH/binAdd the following to
~/.zshrc
export GOPATH="/Users/chloe/workspace/go"
export PATH="$GOPATH:$PATH"+
After setting $GOPATH, you can use the go env command to check if it's properly applied!
- Go downloads and stores downloaded codes in
$GOPATH/srcclassified by downloaded domain- ex) 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
-
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
- ex)
-
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
package main import "fmt" func main() { fmt.Println("Hello World") }
-
-
To export a function in Go, write it in upper-case
-
ex)
something.go
package something import "fmt" func saySeeya() { fmt.Println("See ya!") } func SayHello() { fmt.Println("Hello!") }
main.go
package main import ( "fmt" "github.com/chloe-codes1/go101/something" ) func main() { fmt.Println("Hello World") something.SayHello() }
-
Let's output
ex)
package main
import "fmt"
func main() {
fmt.Print("Hello goorm!")
}-
print / println can be used without importing
fmtpackage -
println and print differ in the presence of line break
- To make a line break with print, use
\n!
- To make a line break with print, use
-
Single quotes don't work!
- Use double quotes
-
print functions can output the results of operations inside functions!
ex)
package main func main() { var num1 int = 1000 var num2 int = 1413 println("Let's do addition ~ ", num1 + num2) }
chloe@chloe-XPS-15-9570 ~/Workspace/Go/Go101 $ ./calc Let's do addition ~ 2413
- Package for input/output
- How to use
- Write
import "fmt"below package main
- Write
+
- gobyexample.com
- tour.golang.org
- blog.golang.org
- dave.cheney.net
- https://golang.org/doc/effective_go.html
- must read!