Homepage / Notes / Computer Science / Programming Languages / Go
go run hello-world.go
to run the file directly go build hello-world.go
to build it and ./hello-world
to run it
import "fmt"
func main() {
.Println("hello world")
fmt}
hello world
import "fmt"
func main() {
var name = "Damien"
.Println(name)
fmt
var number int = 9
.Println(number)
fmt
:= "apple"
fruit .Println(fruit)
fmt}
Damien
9
apple
:=
is shorthand for var name type = "apple"
Using var
makes variables mutable by default:
import "fmt"
func main() {
var name = "Damien"
= "Julien"
name .Println(name)
fmt}
Julien
But const
keyword make they immutable:
import "fmt"
func main() {
const name = "Damien"
= "Julien"
name .Println(name)
fmt}
Throws "cannot assign to name (declared const)"
import "fmt"
func main() {
:= 1
i for i <= 5 {
.Println(i)
fmt= i + 1
i }
}
1
2
3
4
5
You can have an if without an else, but no ternary if.
import "fmt"
func main() {
if true {
.Println("This is true!")
fmt} else {
.Println("This is false!")
fmt}
}
This is true!
import "fmt"
func main() {
:= 2
i switch i {
case 1:
.Println("one")
fmtcase 2:
.Println("two")
fmtcase 3:
.Println("three")
fmtdefault:
.Println("English name is unknown")
fmt}
}
two
Sequence of elements in a specific length. The type of elements and length are both part of the array's type. By default an array is zero-valued.
import "fmt"
func main() {
var a [6]int
.Println(a)
fmt
[2] = 9
a.Println(a)
fmt.Println("first element:", a[0])
fmt.Println("3rd element:", a[2])
fmt
:= [3]int{1, 2, 3}
b .Println("b:", b)
fmt.Println(len(b))
fmt}
[0 0 0 0 0 0]
[0 0 9 0 0 0]
first element: 0
3rd element: 9
b: [1 2 3]
3
More common in Go than arrays. Typed only by the element they contain, not the length.
import "fmt"
func main() {
:= make([]string, 3)
s .Println(s)
fmt
[0] = "a"
s[1] = "b"
s[2] = "c"
s.Println(s)
fmt
= append(s, "d")
s = append(s, "e", "f")
s .Println(s)
fmt
.Println(s[2:4])
fmt.Println(s[:4])
fmt.Println(s[2:])
fmt}
[ ]
[a b c]
[a b c d e f]
[c d]
[a b c d]
[c d e f]
import "fmt"
func main() {
:= make(map[string]int)
m
["price"] = 99
m["discount"] = 10
m.Println(m)
fmt
delete(m, "discount")
.Println(m)
fmt
.Println(m["price"])
fmt
:= map[string]int{"foo": 1, "bar": 2}
n .Println(n)
fmt}
map[discount:10 price:99]
map[price:99]
99
map[bar:2 foo:1]
Allows to iterate over arrays, slice, maps, strings…
import "fmt"
func main() {
:= []int{1, 2, 3}
nums
for _, num := range nums {
.Println(num)
fmt}
for i, c := range "Damien" {
.Println(i, c)
fmt}
}
1
2
3
0 68
1 97
2 109
3 105
4 101
5 110
Requires explicit returns (won't return last statement). When all parameters are of the same type, it can be placed only at the end.
import "fmt"
func plus(a int, b int) int {
return a + b
}
func plusPlus(a, b, c int) int {
return a + b + c
}
func main() {
.Println(plus(1, 2))
fmt.Println(plusPlus(1, 2, 3))
fmt}
3
6
Go functions can return multiple values
import "fmt"
func sum(a, b int) int {
return a + b
}
func product(a, b int) int {
return a * b
}
func sumProduct(a, b int) (int, int) {
return sum(a, b), product(a, b)
}
func main() {
, product := sumProduct(2, 4)
sum.Println("sum:", sum)
fmt.Println("product:", product)
fmt}
sum: 6
product: 8
Functions with any number of trailing arguments.
import "fmt"
func sum(nums ...int) int {
:= 0
total for _, num := range nums {
+= num
total }
return total
}
func main() {
.Println(sum(1, 2, 3))
fmt
:= []int{1, 2, 3, 4}
nums .Println(sum(nums...))
fmt}
6
10
Typed collection of fields Structs are mutable Access structs' field with the dot (.
) notation
import "fmt"
type person struct {
string
name int
age }
func main() {
:= person{"Bob", 20}
b .Println(b)
fmt.Println(b.age)
fmt}
{Bob 20}
20
import (
"fmt"
"strconv"
)
type person struct {
string
name int
age }
func (p *person) id() string {
return p.name + string('-') + strconv.Itoa(p.age)
}
func main() {
:= person{"Bob", 20}
b .Println(b)
fmt.Println(b.id())
fmt}
{Bob 20}
Bob-20
Named collections of struct types
import (
"fmt"
"strconv"
)
type Living interface {
() string
id}
type Person struct {
string
name int
year_of_birth }
type Dog struct {
string
name int
year_of_birth }
func (p Person) id() string {
return "person-" + p.name + string('-') + strconv.Itoa(p.year_of_birth)
}
func (d Dog) id() string {
return "dog-" + d.name + string('-') + strconv.Itoa(d.year_of_birth)
}
func printData(l Living) {
.Println(l)
fmt.Println(l.id())
fmt}
func main() {
(Person{name: "Bob", year_of_birth: 1993})
printData(Dog{name: "Fido", year_of_birth: 2003})
printData}
{Bob 1993}
person-Bob-1993
{Fido 2003}
dog-Fido-2003
import (
"fmt"
)
type Number interface {
~int | ~float32 | ~float64
}
func min[T Number](x, y T) T {
if x < y {
return x
}
return y
}
func main() {
.Println(min(4, 9))
fmt.Println(min(4.5, 1.78))
fmt}
4
1.78
https://github.com/tinygo-org/tinygo
TinyGo is a Go compiler intended for use in small places such as microcontrollers, WebAssembly (Wasm), and command-line tools.
A Framework for Modern CLI Apps in Go
https://github.com/charmbracelet/bubbletea
A powerful little TUI framework
https://github.com/charmbracelet/gum
A tool for glamorous shell scripts 🎀
https://github.com/charmbracelet/huh
A simple, powerful library for building interactive forms and prompts in the terminal.
Build HTML with Go
https://go.dev/doc/effective_go
https://github.com/livebud/bud The Full-Stack Web Framework for Go
https://wails.io/ Build beautiful cross-platform applications using Go
https://github.com/gocopper/copper
Copper is a Go toolkit complete with everything you need to build web apps