Talk by Matej Baćo from Nivas
The Go Programming Language Specification on http://golang.org/ref/spec
Original creators Ken Thompson (Unix, B etc.), Robert Griesemer (V8 etc.) and Rob Pike (Unix, UTF-8 etc.)
It borrows from Unix pipe mechanism and atomic programs (single purpose), is very fast, works great with UTF-8 and has excellent concurrency model.
it has preconceived ideas about what programming language should have or more importantly should not have in order to make programming easier and more practical
It has the MOST FEARSOME mascot in the world!
package main
import (
"log"
"github.com/user/db"
)
func main() {
dbUser, dbPass := "root", "password"
dbConn, err := db.Open(dbUser, dbPass)
if err != nil {
log.Fatalf("DB connect failed with %v", err)
}
defer dbConn.Close()
// ...
}
Don't have db package from last slide?
Just type go get github.com/user/db in command line
as in Unix shell eq.
find . -name ".log" | grep "DEBUG" | xargs rm
// ...
staticHandler := static.NewHandler(ROOT_PATH)
gzipStaticHandler := gzip.NewHandler(staticHandler)
router.PathPrefix("/resources").Handler(gzipStaticHandler)
// ...
updateHandler := update.NewHandler(ROOT_PATH)
updateAuthHandler := auth.NewAuthHandler(updateHandler)
router.PathPrefix("/update").Methods("POST").Handler(updateAuthHandler)
// ...
http.ListenAndServe(":8080", nil)
Goroutines, non-blocking execution of concurrent functions multiplexed onto multiple OS threads, which synchronize and coordinate using special type called channel.
package main
import "fmt"
func main() {
idCh := make(chan int)
go func() {
id := 0
for {
idCh <- id
id = id + 1
}
}()
fmt.Println(<-idCh)
fmt.Println(<-idCh)
fmt.Println(<-idCh)
}
// ...
type fileTask struct {
Path string
Node os.FileInfo
Err error
}
var taskCh chan *fileTask
var wg sync.WaitGroup
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
// ...
taskCh = make(chan *fileTask)
for i := 0; i < 1000; i++ {
go worker(i, taskCh)
}
err := filepath.Walk(inputPath, visit)
// ...
wg.Wait()
}
func visit(path string, node os.FileInfo, err error) error {
task := &fileTask{
Path: path,
Node: node,
Err: err}
wg.Add(1)
taskCh <- task
return nil
}
func worker(workerId int, taskCh <-chan *fileTask) {
for {
task := <-taskCh
// ...
deleted, err := visitLogic(task.Path, task.Node)
// error handling and logging ...
wg.Done()
}
}
// ...
“Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here.”
package io
type Reader interface {
Read(p []byte) (n int, err error)
}
// -----
func deletable(r io.Reader) bool
r, err := os.Open(pathToFile)
del := deletable(r)
// ----
func Open(name string) (file *File, err error)
type File struct {}
func (f *File) Read(b []byte) (n int, err error)
Please give feedback for this talk on
joind.in/13601
Slides will be on the same address.