Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128
type Id = uint64
type ExampleGuy struct {
isCool bool
numberOfCars int32
}
type Something interface {
calc() float64
}
var numbers = []uint32{1, 2, 3, 4}
for _, n := range numbers {
fmt.Println(n)
}
m := map[uint64]bool{
1: true,
2: false,
}
fmt.Println(m[1]) // true
fmt.Println(m[2]) // false
func Calc(a float64, b float64) float64 {
return a + b
}
func Multiply(a string) (float64, error) {
i, err := strconv.ParseFloat(a, 64)
if err != nil {
return 0, err
}
return i * i, nil
}
type Measurements struct {
measures []float64
}
func (m *Measurements) Sum() float64 {
var result float64
for _, measure := range m.measures {
result += measure
}
return result
}
var measures = &Measurements{
measures: []float64{1, 2, 3, 4},
}
measures.Sum()
There are no classes in Golang. Organize your code in packages
// main.go
package main
import (
"github.com/papertrail/sawmill/event"
)
// event/event.go
package event
type Event struct {
id uint64
}
Goroutines are lightweight threads (green threads) managed by the Go runtime.
func main() {
go doSomething()
}
Use channels to transport data.
c := make(chan string)
c <- "hello"
str := <-c
fmt.Println(str) // hello
Channels are blocking
c := make(chan string)
str := <-c
fmt.Println(str) // Not happening
Use for
and select
to read from multiple channels
func (q *Queue) Start() {
tick := time.NewTicker(time.Second * 10)
for {
select {
case measurement := <-q.insertChan:
// Add measurement to queue
break
case <-tick.C:
// Flush queue
break
}
}
}
go queue.Start()
A couple weeks to ramp up. VSCode, Goland, others …
Inserting 75k rows per second into ClickHouse
Create main.go
// main.go
package main
import "fmt"
func main() {
fmt.Println("Hello!")
}
Build it!
$ go build
$ ls -l
total 4128
-rwxr-xr-x 1 lthomas1 staff 2106512 Jan 12 14:38 hello
-rw-r--r-- 1 lthomas1 staff 67 Jan 12 14:36 main.go
Run it!
$ ./hello
Hello!
Compiles quickly
$ time go build
real 0m0.722s
user 0m0.665s
sys 0m0.385s
Fat binaries
$ ls -lh sockets-talk
-rwxr-xr-x 1 lthomas1 staff 6.6M Jan 12 14:16 sockets-talk