GOLang
& GoatCore
Dlaczego powstał?
Jak zacząć
Krok 1
● Zainstaluj mingw64
● C_INCLUDE_PATH
● PATH
C:Program Filesmingw-w64x86_64-6.3.0-posix-seh-rt_v5-rev1mingw64include
C:Program Filesmingw-w64x86_64-6.3.0-posix-seh-rt_v5-rev1mingw64libgccmingw324.5.1include
C:Program Filesmingw-w64x86_64-6.3.0-posix-seh-rt_v5-rev1mingw64bin
C:Program Filesmingw-w64x86_64-6.3.0-posix-seh-rt_v5-rev1mingw64libexecgccx86_64-w64-mingw326.2.0
Krok 2
● Zainstaluj golang
● Zainstaluj git
● Zainstaluj atom
● Zainstaluj go-plus plugin (do atoma)
● Zainstaluj postgresa (lub inną bazę w zależności od projektu)
● Zainstaluj delve
● Zainstaluj npm, yarn...
Krok 3
● Zainstaluj biblioteki dla atoma
> go get -u golang.org/x/tools/cmd/goimports
> go get -u golang.org/x/tools/cmd/gorename
> go get -u github.com/sqs/goreturns
> go get -u github.com/nsf/gocode
> go get -u github.com/alecthomas/gometalinter
> go get -u github.com/zmb3/gogetdoc
> go get -u github.com/rogpeppe/godef
> go get -u golang.org/x/tools/cmd/guru
Krok 4
● Pobieramy projekt
> git clone github.com/goatcms/goatcore
> git clone github.com/goatcms/goatcms
Krok 5
● Pobieramy projekt
> cd github.com/goatcms/goatcms
> go run ./main.go run --loglvl=dev
Jak zacząć szybciej
Pobieramy i odpalamy
● Pobieramy github.com/goatcms/goatcms/devops/devtools
● Odpalamy
> docker-compose up
> git clone https://github.com/goatcms/goatcms.git
Czas na demo
Budowanie
Pobieramy i odpalamy
● Odpalamy
● Budujemy #1
> go build .main.go
> go run ./main.go run --loglvl=dev
Pobieramy i odpalamy
● Budujemy #2
● Budujemy #3
> GOARM=6 GOARCH=arm GOOS=linux go build examples/raspi_blink.go
> go build -ldflags "-w" .main.go
Dlaczego go jest odjechany
“Concurrency Is Not Parallelism”
- Rob Pike
Kanały
package main
import "fmt"
func main() {
ch := make(chan int, 2)
ch <- 1
ch <- 2
fmt.Println(<-ch)
fmt.Println(<-ch)
}
Kanał & runtime
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}
func main() {
c := make(chan int, 10)
go fibonacci(cap(c), c)
for i := range c {
fmt.Println(i)
}
}
Select
func main() {
tick := time.Tick(100 * time.Millisecond)
boom := time.After(500 * time.Millisecond)
for {
select {
case <-tick:
fmt.Println("tick.")
case <-boom:
fmt.Println("BOOM!")
return
default:
fmt.Println(" .")
time.Sleep(50 * time.Millisecond)
}
}
}
GoatCore
… iteracja po plikach
fsloop.NewLoop(&fsloop.LoopData{
Filespace: fs,
FileFilter: func(fs filesystem.Filespace, subPath string) bool {
return strings.HasSuffix(subPath, ".json")
},
OnFile: func(fs filesystem.Filespace, subPath string) error {
data, err := fs.ReadFile(subPath)
if err != nil {
return err
}
tmap := map[string]string{}
if err = LoadJSON("", tmap, data); err != nil {
return err
}
i18.Set(tmap)
return nil
},
}, scope).Run(basePath)
… iteracja po plikach
func LoadJSON(resultKey string, result map[string]string, data []byte) error {
return jsonparser.ObjectEach(data, func(key []byte, value []byte,
dataType jsonparser.ValueType, offset int) error {
var newResultKey string
if resultKey != "" {
newResultKey = resultKey + "." + string(key)
} else {
newResultKey = string(key)
}
switch dataType {
case jsonparser.Object:
return LoadJSON(newResultKey, result, value)
case jsonparser.String:
result[newResultKey] = string(value)
}
return nil
})
}
What makes it so fast?
● It does not rely on encoding/json, reflection or interface{}, the only real
package dependency is bytes.
● Operates with JSON payload on byte level, providing you pointers to the
original data structure: no memory allocation.
● No automatic type conversions, by default everything is a []byte, but it
provides you value type, so you can convert by yourself (there is few
helpers included).
● Does not parse full record, only keys you specified
- buger/jsonparser
Testujemy
● Testy jednostkowe
● Testy wydajnościowe
> go test -v github.com/goatcms/goatcore/...
> cd github.com/goatcms/goatcore/i18n/fsi18loader
> go test --bench=Linear --cpu=1,2,3,4,5,6,7,8,9,10
Pytania?
GoatCore
https://github.com/goatcms/goatcore
Dziękuję za uwagę

GoLang & GoatCore

  • 1.
  • 2.
  • 7.
  • 8.
    Krok 1 ● Zainstalujmingw64 ● C_INCLUDE_PATH ● PATH C:Program Filesmingw-w64x86_64-6.3.0-posix-seh-rt_v5-rev1mingw64include C:Program Filesmingw-w64x86_64-6.3.0-posix-seh-rt_v5-rev1mingw64libgccmingw324.5.1include C:Program Filesmingw-w64x86_64-6.3.0-posix-seh-rt_v5-rev1mingw64bin C:Program Filesmingw-w64x86_64-6.3.0-posix-seh-rt_v5-rev1mingw64libexecgccx86_64-w64-mingw326.2.0
  • 9.
    Krok 2 ● Zainstalujgolang ● Zainstaluj git ● Zainstaluj atom ● Zainstaluj go-plus plugin (do atoma) ● Zainstaluj postgresa (lub inną bazę w zależności od projektu) ● Zainstaluj delve ● Zainstaluj npm, yarn...
  • 10.
    Krok 3 ● Zainstalujbiblioteki dla atoma > go get -u golang.org/x/tools/cmd/goimports > go get -u golang.org/x/tools/cmd/gorename > go get -u github.com/sqs/goreturns > go get -u github.com/nsf/gocode > go get -u github.com/alecthomas/gometalinter > go get -u github.com/zmb3/gogetdoc > go get -u github.com/rogpeppe/godef > go get -u golang.org/x/tools/cmd/guru
  • 11.
    Krok 4 ● Pobieramyprojekt > git clone github.com/goatcms/goatcore > git clone github.com/goatcms/goatcms
  • 12.
    Krok 5 ● Pobieramyprojekt > cd github.com/goatcms/goatcms > go run ./main.go run --loglvl=dev
  • 13.
  • 14.
    Pobieramy i odpalamy ●Pobieramy github.com/goatcms/goatcms/devops/devtools ● Odpalamy > docker-compose up > git clone https://github.com/goatcms/goatcms.git
  • 15.
  • 16.
  • 17.
    Pobieramy i odpalamy ●Odpalamy ● Budujemy #1 > go build .main.go > go run ./main.go run --loglvl=dev
  • 18.
    Pobieramy i odpalamy ●Budujemy #2 ● Budujemy #3 > GOARM=6 GOARCH=arm GOOS=linux go build examples/raspi_blink.go > go build -ldflags "-w" .main.go
  • 19.
  • 20.
    “Concurrency Is NotParallelism” - Rob Pike
  • 21.
    Kanały package main import "fmt" funcmain() { ch := make(chan int, 2) ch <- 1 ch <- 2 fmt.Println(<-ch) fmt.Println(<-ch) }
  • 22.
    Kanał & runtime funcfibonacci(n int, c chan int) { x, y := 0, 1 for i := 0; i < n; i++ { c <- x x, y = y, x+y } close(c) } func main() { c := make(chan int, 10) go fibonacci(cap(c), c) for i := range c { fmt.Println(i) } }
  • 23.
    Select func main() { tick:= time.Tick(100 * time.Millisecond) boom := time.After(500 * time.Millisecond) for { select { case <-tick: fmt.Println("tick.") case <-boom: fmt.Println("BOOM!") return default: fmt.Println(" .") time.Sleep(50 * time.Millisecond) } } }
  • 24.
  • 25.
    … iteracja poplikach fsloop.NewLoop(&fsloop.LoopData{ Filespace: fs, FileFilter: func(fs filesystem.Filespace, subPath string) bool { return strings.HasSuffix(subPath, ".json") }, OnFile: func(fs filesystem.Filespace, subPath string) error { data, err := fs.ReadFile(subPath) if err != nil { return err } tmap := map[string]string{} if err = LoadJSON("", tmap, data); err != nil { return err } i18.Set(tmap) return nil }, }, scope).Run(basePath)
  • 26.
    … iteracja poplikach func LoadJSON(resultKey string, result map[string]string, data []byte) error { return jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error { var newResultKey string if resultKey != "" { newResultKey = resultKey + "." + string(key) } else { newResultKey = string(key) } switch dataType { case jsonparser.Object: return LoadJSON(newResultKey, result, value) case jsonparser.String: result[newResultKey] = string(value) } return nil }) }
  • 27.
    What makes itso fast? ● It does not rely on encoding/json, reflection or interface{}, the only real package dependency is bytes. ● Operates with JSON payload on byte level, providing you pointers to the original data structure: no memory allocation. ● No automatic type conversions, by default everything is a []byte, but it provides you value type, so you can convert by yourself (there is few helpers included). ● Does not parse full record, only keys you specified - buger/jsonparser
  • 28.
    Testujemy ● Testy jednostkowe ●Testy wydajnościowe > go test -v github.com/goatcms/goatcore/... > cd github.com/goatcms/goatcore/i18n/fsi18loader > go test --bench=Linear --cpu=1,2,3,4,5,6,7,8,9,10
  • 29.
  • 30.
  • 31.