Are we ready to Go?
-
Upload
adam-dudczak -
Category
Technology
-
view
456 -
download
0
description
Transcript of Are we ready to Go?
Are we ready to Go?Adam Dudczak
Tomasz Jackowiak
Disclaimer
Who we are?
Why Go?
Heka? Etcd? InfluxDb? JuJu?
Top 10?
Not yet ;-)
Why Go?
Been there, done thatB, C, Java, JavaScript …
• Class free, but object oriented
• Statically-typed - with elem. of duck typing
• Garbage collector
• Compiled - really fast compiler
• Open source from up to bottom (BSD license)
• Write once, run anywhere
Show me the code!!!
package main !import "fmt" !func main() { fmt.Println("Hello, World") }
go run
package main !import "fmt" !func swap(x, y string) (string, string) { return y, x } !func main() { a, b := swap("hello", "world") fmt.Println(a, b) }
package main !import ( "fmt" "math" ) !type Vertex struct { X, Y float64 } !func (v *Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) } !func main() { v := &Vertex{3, 4} fmt.Println(v.Abs()) }
Microservice!?!
package main !import ( "io" "net/http" ) !func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello, world\n") }) http.ListenAndServe(":8080", nil) }
type Quote struct { BookTitle string `json:"book_title"` Sentence string `json:"sentence"` } !func NewQuotes(quotesFilePath string) ([]Quote, error) { fileContent, _ := os.Open(quotesFilePath) defer fileContent.Close() ! var quotes []Quote decoder := json.NewDecoder(fileContent) if err := decoder.Decode("es); err != nil { return nil, errors.New("Failed to decode json") } return quotes, nil }
type TwitterClient interface { Tweets(screenName string) []string } !!type FakeClient struct{} !func (fake *FakeClient) Tweets(screenName string) []string { return []string{"test tweet"} } !!func Test_shouldCreateNewPaolo(t *testing.T) { twitterClient := &FakeClient{} paolo := NewTweetingPaolo(twitterClient) assert.NotNil(t, paolo) }
go routines
func sleepySort(number int) { time.Sleep(time.Duration(number)) fmt.Println(number) } !func main() { arrayToSort := []int{12, 14, 3, 6, 1, 2} for _, val := range arrayToSort { sleepySort(val) } }
func sleepySort(number int) { time.Sleep(time.Duration(number)) fmt.Println(number) } !func main() { arrayToSort := []int{12, 14, 3, 6, 1, 2} for _, val := range arrayToSort { sleepySort(val) } }
output: 12, 14, 3, 6, 1, 2
func sleepySort(number int) { time.Sleep(time.Duration(number)) fmt.Println(number) } !func main() { arrayToSort := []int{12, 14, 3, 6, 1, 2} for _, val := range arrayToSort { go sleepySort(val) } }
func sleepySort(number int) { time.Sleep(time.Duration(number)) fmt.Println(number) } !func main() { arrayToSort := []int{12, 14, 3, 6, 1, 2} for _, val := range arrayToSort { go sleepySort(val) } }
output: —
func sleepySort(number int) { time.Sleep(time.Duration(number)) fmt.Println(number) } !func main() { arrayToSort := []int{12, 14, 3, 6, 1, 2} for _, val := range arrayToSort { go sleepySort(val) } time.Sleep(1 * time.Second) }
output: 1, 2, 3, 6, 12, 14
channels
func sleepySort(number int, sort chan<- int) { time.Sleep(time.Duration(number)) sort <- number } !func main() { arrayToSort := []int{12, 14, 3, 6, 1, 2} sort := make(chan int) ! for _, val := range arrayToSort { go sleepySort(val, sort) } ! for i := 0; i < len(arrayToSort); i++ { fmt.Println(<-sort) } }
quote := make(chan Quote) go func() { quote <- localPaolo.RandomQuote() }() go func() { quote <- twittingPaolo.RandomTweet() }() go func() { quote <- wisePaolo.RandomHeartQuote() }() !for i := 0; i < 3; i++ { fmt.Println(<-quote) }
go get github.com/tjackowiak/coelho
Coding standards
gofmt & co
github / Drone.io / coveralls
Are we ready to Go?
Pros• Standing on the shoulders of giant
• Concise and readable - maybe even fun ;-)
• Simple and powerful tooling (go run | install | …)
• Really nice dependency management
• Language spec - with promise of backward compatibility
Cons• Relatively new language
• There is still space to improve how GC works
• Not a JVM language
It is definately worth trying ;-)
@maneo, @kevorin
Questions?