SlideShare uma empresa Scribd logo
1 de 58
Baixar para ler offline
Go Lang Tutorial
Wei-Ning Huang (AZ)
About the Speaker
● Wei-Ning Huang (a.k.a: AZ)
● Open source / Freelance Developer
● Found out more at http://azhuang.me
About the Slide
● License:
● Most of the code examples come from
official Go Tour and Effective Go
Outline
● History
● Why Go?
● Library Support
● Tutorial
○ Variable Declaration
○ Function Declaration
○ Flow Control
○ Method and Interface
○ Goroutines
● Conclusion
● References
Go!
History
● From Google!
○ The guys at Plan 9 from Bell Labs
● Current Version 1.1.2, still being actively
developed
Why Go!
● Fast
○ Almost as fast as C
○ Faster than C in some cases
● A small language
● Statically-typed
● Garbage Collection
● Easy to write/learn
● Reflection
How fast is Go?
From Computer Language Benchmark Game
Who use Go?
● Google, of course
● Google App Engine also supports Go!
Library Support
● A lot of built-in support including:
○ Containers: container/heap, container/list
○ Web server: net/http
○ Cryptography: crypto/md5, crypto/sha1
○ Compression: compress/gzip
○ Database: database/sql
Third-Party Libraries
● Gorilla: Web Framework
● go-qt: Qt binding for Go
● go-gtk3: Gtk3 binding for Go
● go-opengl: OpenGL binding for Go
● go:ngine: Go 3D Engine
● mgo: MongoDB binding for go
Tons more at:
https://code.google.com/p/go-wiki/wiki/Projects
Hello World!
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
$ go run hello_word.go
Hello, 世界
Using Package
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is",
rand.Intn(10))
}
You can also import package with separated line
import "fmt"
import "math/rand"
● In Go, a name is exported if it begins with
capital letter. That is, you should use
instead of
Exported Names
fmt.Println("My favorite number is", rand.
Intn(10))
fmt.println("My favorite number is", rand.
Intn(10))
Variables
package main
import "fmt"
var x, y, z int
func main() {
var c, python, java = true, false, "no!"
haskell, racket, sml := true, true, false
fmt.Println(x, y, z, c, python, java,
haskell, racket, sml)
}
type can be omitted, since compiler
can guess it from the initializer
using the := construct, we can omit the
var keyword and type!
Basic Types
● 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
var c complex128 = 1 + 2i
Constants
package main
import "fmt"
const Pi = 3.14
func main() {
const World = "世界"
fmt.Println("Hello", World)
fmt.Println("Happy", Pi, "Day")
const Truth = true
fmt.Println("Go rules?", Truth)
}
Grouping Variable and Constants
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
const (
Big = 1 << 100
Small = Big >> 99
)
Functions
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func add_and_sub(x, y int) (int, int) {
return x + y, x -y
}
func main() {
fmt.Println(add(42, 13))
a, b := add_and_sub(42, 13)
fmt.Println(a, b)
}
paramter name type return type
x, y share the same type multiple return value
receiving multiple reutrn value
Functions - Named results
package main
import "fmt"
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
func main() {
fmt.Println(split(17))
}
Functions are First Class Citizens
func main() {
hypot := func(x, y float64) float64 {
return math.Sqrt(x*x + y*y)
}
fmt.Println(hypot(3, 4))
}
Functions in Go are first class citizens, which means that you can
pass it around like values.
create a lambda(anonymous) function and assign it to
a variable
Closures
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
Go uses lexical closures, which means a function is evaluated in the
environment where it is defined.
sum is accessible inside the
anonymous function!
For loop
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
for ; sum < 1000; {
sum += sum
}
for sum < 10000 {
sum += sum
}
fmt.Println(sum)
for {
}
}
Go does not have while loop!
while loops are for loops!
Infinite loop!
similar to C but without parentheses
Condition: If
package main
import (
"fmt"
)
func main() {
a := 10
if a > 1 {
fmt.Println("if true")
}
if b := 123 * 456; b < 10000 {
fmt.Println("another one, but with short
statement")
}
}
short statement, b is lives only inside if block
Condition: Switch
func main() {
fmt.Print("Go runs on ")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X.")
case "linux":
fmt.Println("Linux.")
default:
// freebsd, openbsd,
// plan9, windows...
fmt.Printf("%s.", os)
}
}
Just like if, can have
short statement
Switch cases evaluate cases from top to bottom, stopping when a
case succeeds.
Switch Without Condition
func main() {
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
Like writing long if/else chains
no condition
Structs
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
v.X = 4
fmt.Println(v.X)
}
A struct is a collection of fields.
Struct fields are accessed using a dot.
Structs
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
v.X = 4
p := &v
fmt.Println(v.X, p.X)
}
p is a pointer to v,
which has type *Vertex
Use dot to access
field of a pointer
The new Function
package main
import "fmt"
type Vertex struct {
X, Y int
}
func main() {
v := new(Vertex)
fmt.Println(v)
v.X, v.Y = 11, 9
fmt.Println(v)
}
The expression new(T) allocates a zeroed T value and returns a
pointer to it.
v has type: *Vertex
Slices
func main() {
p := []int{2, 3, 5, 7, 11, 13}
fmt.Println("p ==", p)
fmt.Println("p[1:4] ==", p[1:4]) // [3 5 7]
fmt.Println("p[:3] ==", p[:3]) // [2 3 5]
fmt.Println("p[4:] ==", p[4:]) // [11 13]
for i := 0; i < len(p); i++ {
fmt.Printf("p[%d] == %dn", i, p[i])
}
var z []int
if z == nil {
fmt.Println("nil value")
}
}
A slice points to an array of values and also includes a length.
The zero value of a slice is nil
empty slice: nil
Making Slices
func main() {
a := make([]int, 5)
printSlice("a", a)
b := make([]int, 0, 5)
printSlice("b", b)
}
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %vn",
s, len(x), cap(x), x)
}
Use the make function to create slices.
make(type, length, capacity)
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
Array V.S. Slices
func main() {
var array = [...]string{"a", "b", "c"}
var slice = []string{"a", "b", "c"}
fmt.Printf("%Tn", array)
fmt.Printf("%Tn", slice)
//array = append(array, array...) // Won't
compile!
slice = append(slice, slice...)
fmt.Println(slice)
}
[3]string
[]string
[a b c a b c]
Maps
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m map[string]Vertex
func main() {
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, -74.39967,
}
fmt.Println(m["Bell Labs"])
}
● A map maps keys to values.
● Maps must be created with make (not new) before use; the nil
map is empty and cannot be assigned to.
Maps Literals
var m = map[string]Vertex{
"Bell Labs": Vertex{
40.68433, -74.39967,
},
"Google": Vertex{
37.42202, -122.08408,
},
}
var m2 = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
"Google": {37.42202, -122.08408}
}
map[key_type]value_type{
key1: value1, key2: value2 …
}
Can omit type name
Mutating Maps
m := make(map[string]int)
m["Answer"] = 42
Assigning
m, ok := m["Answer"]
if ok {
fmt.Println("key exists!")
}
Testing if a key exists
delete(m, "Answer")
Deleting an element
Iterating through Slices or Maps
package main
import "fmt"
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
var m = map[string]int{"a": 1, "b": 2}
func main() {
for i, v := range pow {
fmt.Printf("2**%d = %dn", i, v)
}
for k, v := range m {
fmt.Printf("%s: %dn", k, v)
}
}
Use the range keyword
The _ Variable
package main
import "fmt"
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
var m = map[string]int{"a": 1, "b": 2}
func main() {
for _, v := range pow {
fmt.Printf("%dn", v)
}
for _, v := range m {
fmt.Printf("%dn", v)
}
}
Go won’t let you compile you declare some variable but didn’t use it.
In this case, you can assign it to _ to indicate that it will not be used.
We don’t use index
here, assign it to _.
We don’t use key here,
assign it to _.
Go: Objects without Class
● No class, use interfaces instead
● No inheritance, use embedding instead
Method Declaration
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())
}
Basically look like functions, but with extra receiver declaration
before function name.
Method Declaration (cont’d)
func (t time.Time) bla() string {
return t.LocalTime().String()[0:5]
}
You may be tempted to declare new method on existing type, like
monkey patching in Python or Ruby.
cannot define new methods on non-local type time.
Time
type MyTime time.Time
func (t MyTime) bla() string {
return t.LocalTime().String()[0:5]
}
The correct way is to create a type synonym then define method on
it.
Interfaces
type Abser interface {
Abs() float64
}
type MyFloat float64
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}
● An interface type is defined by a set of methods.
● A value of interface type can hold any value that implements
those methods.
● Interfaces are satisfied implicitly
○ capture idioms and patterns after the code is being written
type Vertex struct {
X, Y float64
}
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func main() {
var a Abser
f := MyFloat(-math.Sqrt2)
v := Vertex{3, 4}
a = f // a MyFloat implements Abser
a = &v // a *Vertex implements Abser
a = v // a Vertex, does NOT implement Abser
fmt.Println(a.Abs())
}
Interfaces (cont’d)
Interfaces (cont’d)
● Interfaces are infact like static-checked duck typing!
● When I see a bird that walks like a duck and swims like a duck
and quacks like a duck, I call that bird a duck.
package main
import "fmt"
type World struct{}
func (w *World) String() string {
return "世界"
}
func main() {
fmt.Printf("Hello, %sn", new(World))
}
Interfaces: Another Example
% go run hello.go
Hello, 世界
package fmt
// Stringer is implemented by any value that has a
String method,
// which defines the ``native'' format for that
value.
// The String method is used to print values passed
as an operand
// to any format that accepts a string or to an
unformatted printer
// such as Print.
type Stringer interface{
String() string
}
Interfaces: Another Example (cont’d)
fmt.Stringer
Naming Convention: An interface with single method
should be name as method name with “er” as postfix
Errors
type MyError struct {
When time.Time
What string
}
func (e *MyError) Error() string {
return fmt.Sprintf("at %v, %s",
e.When, e.What)
}
type error interface {
Error() string
}
Errors in go are defined as interface:
Defining new error type
Embedding
type Car struct {
wheelCount int
}
func (car Car) numberOfWheels() int {
return car.wheelCount
}
type Ferrari struct {
Car //anonymous field Car
}
func main() {
f := Ferrari{Car{4}}
fmt.Println("A Ferrari has this many wheels: ",
f.numberOfWheels())
}
Sort of “Inheritance” in OOP.
Goroutines
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
A goroutine is a lightweight thread managed by the Go runtime.
Channels
ch := make(chan int)
ch <- v // Send v to channel ch.
v := <-ch // Receive from ch, and
// assign value to v.
● Channels are a typed conduit through which you can send and
receive values.
● By default, sends and receives block until the other side is
ready.
● Channels can be buffered. Provide the buffer length as the
second argument to make to initialize a buffered channel:
● Sends to a buffered channel block only when the buffer is full.
Receives block when the buffer is empty.
ch := make(chan int, 100)
Channels (cont’)
func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
c <- sum // send sum to c
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
}
Closing a Channel
close(ch) // close the channel
v, ok := <-ch
if !ok {
fmt.Println("channel closed")
}
● A sender can close a channel to indicate that no more values
will be sent.
● Receivers can test whether a channel has been closed by
assigning a second parameter to the receive expression
Select
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}
The select statement lets a goroutine wait on multiple communication
operations.
Defer, Panic, and Recover
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
ofs, err := os.Create("/dev/stdout")
if err == nil {
fmt.Fprintf(ofs, "Hello, 世界n")
} else {
panic(err)
}
defer ofs.Close()
}
A defer statement pushes a function call onto a list. The list of
saved calls is executed after the surrounding function returns.
Generics?
Why does Go not have generic types?
Generics may well be added at some point. We don't feel an urgency
for them, although we understand some programmers do.
Generics are convenient but they come at a cost in complexity in the
type system and run-time. We haven't yet found a design that gives
value proportionate to the complexity, although we continue to think
about it. Meanwhile, Go's built-in maps and slices, plus the ability to
use the empty interface to construct containers (with explicit
unboxing) mean in many cases it is possible to write code that does
what generics would enable, if less smoothly.
From Go website:
Type Switch
func anything(data interface{})
{
switch data.(type) {
case int:
fmt.Println( "int")
case string:
fmt.Println( "string")
case fmt.Stringer:
fmt.Println( "Stringer")
default:
fmt.Println( "whatever...")
}
}
type World struct{}
func (w *World) String() string {
return "世界"
}
func main() {
anything("a string")
anything(42)
anything(new(World))
anything(make([]int, 5))
}
string
int
Stringer
whatever...
Type Assertion
func anything(data interface{}) {
s, ok := data.(fmt.Stringer)
if ok {
fmt.Printf( "We got Stringer:
%Tn", s)
} else {
fmt.Println( "bah!")
}
}
type World struct{}
func (w *World) String() string
{
return "世界"
}
func main() {
anything("a string")
anything(42)
anything(new(World))
anything(make([]int, 5))
}
bah!
bah!
We got Stringer: *main.World
bah!
Conclusion
● Go is a fast, small, easy-to-learn language
● Actively developed by Google
● Tons of third-party libraries
● Learn it by using it!
References
● Go Official Tutorial [website]
● Effective Go [website]
● Advanced Go Concurrency Patterns [Slide, Video]
● Go and Rust — objects without class [lwn.net]

Mais conteúdo relacionado

Mais procurados

Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golangBasil N G
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golangBo-Yi Wu
 
Go Programming Language by Google
Go Programming Language by GoogleGo Programming Language by Google
Go Programming Language by GoogleUttam Gandhi
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programmingMahmoud Masih Tehrani
 
Golang (Go Programming Language)
Golang (Go Programming Language)Golang (Go Programming Language)
Golang (Go Programming Language)ShubhamMishra485
 
Introduction to GoLang
Introduction to GoLangIntroduction to GoLang
Introduction to GoLangNVISIA
 
Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in GolangOliver N
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practiceGuilherme Garnier
 
GO programming language
GO programming languageGO programming language
GO programming languagetung vu
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrencyjgrahamc
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event LoopDesignveloper
 

Mais procurados (20)

Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
 
Go Programming Language by Google
Go Programming Language by GoogleGo Programming Language by Google
Go Programming Language by Google
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
Go Language presentation
Go Language presentationGo Language presentation
Go Language presentation
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
 
Golang (Go Programming Language)
Golang (Go Programming Language)Golang (Go Programming Language)
Golang (Go Programming Language)
 
GoLang Introduction
GoLang IntroductionGoLang Introduction
GoLang Introduction
 
Rust vs C++
Rust vs C++Rust vs C++
Rust vs C++
 
Introduction to GoLang
Introduction to GoLangIntroduction to GoLang
Introduction to GoLang
 
Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in Golang
 
Go lang
Go langGo lang
Go lang
 
Golang workshop
Golang workshopGolang workshop
Golang workshop
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practice
 
GO programming language
GO programming languageGO programming language
GO programming language
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Rust Primer
Rust PrimerRust Primer
Rust Primer
 
Rust programming-language
Rust programming-languageRust programming-language
Rust programming-language
 
Concurrency With Go
Concurrency With GoConcurrency With Go
Concurrency With Go
 

Destaque

EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物Wei-Ning Huang
 
ProjectTox: Free as in freedom Skype replacement
ProjectTox: Free as in freedom Skype replacementProjectTox: Free as in freedom Skype replacement
ProjectTox: Free as in freedom Skype replacementWei-Ning Huang
 
Chromium OS Introduction
Chromium OS IntroductionChromium OS Introduction
Chromium OS IntroductionWei-Ning Huang
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about goDvir Volk
 
An introduction to go programming language
An introduction to go programming languageAn introduction to go programming language
An introduction to go programming languageTechnology Parser
 
Google Go! language
Google Go! languageGoogle Go! language
Google Go! languageAndré Mayer
 
Go: Beyond the Basics
Go: Beyond the BasicsGo: Beyond the Basics
Go: Beyond the BasicsJoey Gibson
 
Actor-based concurrency and Akka Fundamentals
Actor-based concurrency and Akka FundamentalsActor-based concurrency and Akka Fundamentals
Actor-based concurrency and Akka FundamentalsNgoc Dao
 
淺談 Groovy 與 AWS 雲端應用開發整合
淺談 Groovy 與 AWS 雲端應用開發整合淺談 Groovy 與 AWS 雲端應用開發整合
淺談 Groovy 與 AWS 雲端應用開發整合Kyle Lin
 
Golang 개발 환경(profile) 다르게 잡아주기
Golang 개발 환경(profile) 다르게 잡아주기Golang 개발 환경(profile) 다르게 잡아주기
Golang 개발 환경(profile) 다르게 잡아주기라한사 아
 
Mongo db - How we use Go and MongoDB by Sam Helman
Mongo db - How we use Go and MongoDB by Sam HelmanMongo db - How we use Go and MongoDB by Sam Helman
Mongo db - How we use Go and MongoDB by Sam HelmanHakka Labs
 
Let's Go (golang)
Let's Go (golang)Let's Go (golang)
Let's Go (golang)상욱 송
 
Spark Summit EU 2015: Matei Zaharia keynote
Spark Summit EU 2015: Matei Zaharia keynoteSpark Summit EU 2015: Matei Zaharia keynote
Spark Summit EU 2015: Matei Zaharia keynoteDatabricks
 
Spark Summit EU 2015: Combining the Strengths of MLlib, scikit-learn, and R
Spark Summit EU 2015: Combining the Strengths of MLlib, scikit-learn, and RSpark Summit EU 2015: Combining the Strengths of MLlib, scikit-learn, and R
Spark Summit EU 2015: Combining the Strengths of MLlib, scikit-learn, and RDatabricks
 
淺談 Geb 網站自動化測試(JCConf 2014)
淺談 Geb 網站自動化測試(JCConf 2014)淺談 Geb 網站自動化測試(JCConf 2014)
淺談 Geb 網站自動化測試(JCConf 2014)Kyle Lin
 

Destaque (20)

EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
 
ProjectTox: Free as in freedom Skype replacement
ProjectTox: Free as in freedom Skype replacementProjectTox: Free as in freedom Skype replacement
ProjectTox: Free as in freedom Skype replacement
 
Code & Beer Opening
Code & Beer OpeningCode & Beer Opening
Code & Beer Opening
 
Chromium OS Introduction
Chromium OS IntroductionChromium OS Introduction
Chromium OS Introduction
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about go
 
An introduction to go programming language
An introduction to go programming languageAn introduction to go programming language
An introduction to go programming language
 
Google Go! language
Google Go! languageGoogle Go! language
Google Go! language
 
Golang
GolangGolang
Golang
 
Go: Beyond the Basics
Go: Beyond the BasicsGo: Beyond the Basics
Go: Beyond the Basics
 
Go lang - What is that thing?
Go lang - What is that thing?Go lang - What is that thing?
Go lang - What is that thing?
 
Actor-based concurrency and Akka Fundamentals
Actor-based concurrency and Akka FundamentalsActor-based concurrency and Akka Fundamentals
Actor-based concurrency and Akka Fundamentals
 
淺談 Groovy 與 AWS 雲端應用開發整合
淺談 Groovy 與 AWS 雲端應用開發整合淺談 Groovy 與 AWS 雲端應用開發整合
淺談 Groovy 與 AWS 雲端應用開發整合
 
Golang 개발 환경(profile) 다르게 잡아주기
Golang 개발 환경(profile) 다르게 잡아주기Golang 개발 환경(profile) 다르게 잡아주기
Golang 개발 환경(profile) 다르게 잡아주기
 
Mongo db - How we use Go and MongoDB by Sam Helman
Mongo db - How we use Go and MongoDB by Sam HelmanMongo db - How we use Go and MongoDB by Sam Helman
Mongo db - How we use Go and MongoDB by Sam Helman
 
Let's Go (golang)
Let's Go (golang)Let's Go (golang)
Let's Go (golang)
 
Akka - A Brief Intro
Akka - A Brief IntroAkka - A Brief Intro
Akka - A Brief Intro
 
Spark Summit EU 2015: Matei Zaharia keynote
Spark Summit EU 2015: Matei Zaharia keynoteSpark Summit EU 2015: Matei Zaharia keynote
Spark Summit EU 2015: Matei Zaharia keynote
 
H20: A platform for big math
H20: A platform for big math H20: A platform for big math
H20: A platform for big math
 
Spark Summit EU 2015: Combining the Strengths of MLlib, scikit-learn, and R
Spark Summit EU 2015: Combining the Strengths of MLlib, scikit-learn, and RSpark Summit EU 2015: Combining the Strengths of MLlib, scikit-learn, and R
Spark Summit EU 2015: Combining the Strengths of MLlib, scikit-learn, and R
 
淺談 Geb 網站自動化測試(JCConf 2014)
淺談 Geb 網站自動化測試(JCConf 2014)淺談 Geb 網站自動化測試(JCConf 2014)
淺談 Geb 網站自動化測試(JCConf 2014)
 

Semelhante a Go Lang Tutorial

Semelhante a Go Lang Tutorial (20)

golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my day
 
Let's golang
Let's golangLet's golang
Let's golang
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
About Go
About GoAbout Go
About Go
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
Burrowing through go! the book
Burrowing through go! the bookBurrowing through go! the book
Burrowing through go! the book
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
Beauty and Power of Go
Beauty and Power of GoBeauty and Power of Go
Beauty and Power of Go
 
Go serving: Building server app with go
Go serving: Building server app with goGo serving: Building server app with go
Go serving: Building server app with go
 
ProgrammingwithGOLang
ProgrammingwithGOLangProgrammingwithGOLang
ProgrammingwithGOLang
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Último (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Go Lang Tutorial

  • 2. About the Speaker ● Wei-Ning Huang (a.k.a: AZ) ● Open source / Freelance Developer ● Found out more at http://azhuang.me
  • 3. About the Slide ● License: ● Most of the code examples come from official Go Tour and Effective Go
  • 4. Outline ● History ● Why Go? ● Library Support ● Tutorial ○ Variable Declaration ○ Function Declaration ○ Flow Control ○ Method and Interface ○ Goroutines ● Conclusion ● References
  • 5. Go!
  • 6. History ● From Google! ○ The guys at Plan 9 from Bell Labs ● Current Version 1.1.2, still being actively developed
  • 7. Why Go! ● Fast ○ Almost as fast as C ○ Faster than C in some cases ● A small language ● Statically-typed ● Garbage Collection ● Easy to write/learn ● Reflection
  • 8. How fast is Go? From Computer Language Benchmark Game
  • 9. Who use Go? ● Google, of course ● Google App Engine also supports Go!
  • 10. Library Support ● A lot of built-in support including: ○ Containers: container/heap, container/list ○ Web server: net/http ○ Cryptography: crypto/md5, crypto/sha1 ○ Compression: compress/gzip ○ Database: database/sql
  • 11. Third-Party Libraries ● Gorilla: Web Framework ● go-qt: Qt binding for Go ● go-gtk3: Gtk3 binding for Go ● go-opengl: OpenGL binding for Go ● go:ngine: Go 3D Engine ● mgo: MongoDB binding for go Tons more at: https://code.google.com/p/go-wiki/wiki/Projects
  • 12. Hello World! package main import "fmt" func main() { fmt.Println("Hello, 世界") } $ go run hello_word.go Hello, 世界
  • 13. Using Package package main import ( "fmt" "math/rand" ) func main() { fmt.Println("My favorite number is", rand.Intn(10)) } You can also import package with separated line import "fmt" import "math/rand"
  • 14. ● In Go, a name is exported if it begins with capital letter. That is, you should use instead of Exported Names fmt.Println("My favorite number is", rand. Intn(10)) fmt.println("My favorite number is", rand. Intn(10))
  • 15. Variables package main import "fmt" var x, y, z int func main() { var c, python, java = true, false, "no!" haskell, racket, sml := true, true, false fmt.Println(x, y, z, c, python, java, haskell, racket, sml) } type can be omitted, since compiler can guess it from the initializer using the := construct, we can omit the var keyword and type!
  • 16. Basic Types ● 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 var c complex128 = 1 + 2i
  • 17. Constants package main import "fmt" const Pi = 3.14 func main() { const World = "世界" fmt.Println("Hello", World) fmt.Println("Happy", Pi, "Day") const Truth = true fmt.Println("Go rules?", Truth) }
  • 18. Grouping Variable and Constants var ( ToBe bool = false MaxInt uint64 = 1<<64 - 1 z complex128 = cmplx.Sqrt(-5 + 12i) ) const ( Big = 1 << 100 Small = Big >> 99 )
  • 19. Functions package main import "fmt" func add(x int, y int) int { return x + y } func add_and_sub(x, y int) (int, int) { return x + y, x -y } func main() { fmt.Println(add(42, 13)) a, b := add_and_sub(42, 13) fmt.Println(a, b) } paramter name type return type x, y share the same type multiple return value receiving multiple reutrn value
  • 20. Functions - Named results package main import "fmt" func split(sum int) (x, y int) { x = sum * 4 / 9 y = sum - x return } func main() { fmt.Println(split(17)) }
  • 21. Functions are First Class Citizens func main() { hypot := func(x, y float64) float64 { return math.Sqrt(x*x + y*y) } fmt.Println(hypot(3, 4)) } Functions in Go are first class citizens, which means that you can pass it around like values. create a lambda(anonymous) function and assign it to a variable
  • 22. Closures func adder() func(int) int { sum := 0 return func(x int) int { sum += x return sum } } Go uses lexical closures, which means a function is evaluated in the environment where it is defined. sum is accessible inside the anonymous function!
  • 23. For loop func main() { sum := 0 for i := 0; i < 10; i++ { sum += i } for ; sum < 1000; { sum += sum } for sum < 10000 { sum += sum } fmt.Println(sum) for { } } Go does not have while loop! while loops are for loops! Infinite loop! similar to C but without parentheses
  • 24. Condition: If package main import ( "fmt" ) func main() { a := 10 if a > 1 { fmt.Println("if true") } if b := 123 * 456; b < 10000 { fmt.Println("another one, but with short statement") } } short statement, b is lives only inside if block
  • 25. Condition: Switch func main() { fmt.Print("Go runs on ") switch os := runtime.GOOS; os { case "darwin": fmt.Println("OS X.") case "linux": fmt.Println("Linux.") default: // freebsd, openbsd, // plan9, windows... fmt.Printf("%s.", os) } } Just like if, can have short statement Switch cases evaluate cases from top to bottom, stopping when a case succeeds.
  • 26. Switch Without Condition func main() { t := time.Now() switch { case t.Hour() < 12: fmt.Println("Good morning!") case t.Hour() < 17: fmt.Println("Good afternoon.") default: fmt.Println("Good evening.") } } Like writing long if/else chains no condition
  • 27. Structs package main import "fmt" type Vertex struct { X int Y int } func main() { v := Vertex{1, 2} v.X = 4 fmt.Println(v.X) } A struct is a collection of fields. Struct fields are accessed using a dot.
  • 28. Structs package main import "fmt" type Vertex struct { X int Y int } func main() { v := Vertex{1, 2} v.X = 4 p := &v fmt.Println(v.X, p.X) } p is a pointer to v, which has type *Vertex Use dot to access field of a pointer
  • 29. The new Function package main import "fmt" type Vertex struct { X, Y int } func main() { v := new(Vertex) fmt.Println(v) v.X, v.Y = 11, 9 fmt.Println(v) } The expression new(T) allocates a zeroed T value and returns a pointer to it. v has type: *Vertex
  • 30. Slices func main() { p := []int{2, 3, 5, 7, 11, 13} fmt.Println("p ==", p) fmt.Println("p[1:4] ==", p[1:4]) // [3 5 7] fmt.Println("p[:3] ==", p[:3]) // [2 3 5] fmt.Println("p[4:] ==", p[4:]) // [11 13] for i := 0; i < len(p); i++ { fmt.Printf("p[%d] == %dn", i, p[i]) } var z []int if z == nil { fmt.Println("nil value") } } A slice points to an array of values and also includes a length. The zero value of a slice is nil empty slice: nil
  • 31. Making Slices func main() { a := make([]int, 5) printSlice("a", a) b := make([]int, 0, 5) printSlice("b", b) } func printSlice(s string, x []int) { fmt.Printf("%s len=%d cap=%d %vn", s, len(x), cap(x), x) } Use the make function to create slices. make(type, length, capacity) a len=5 cap=5 [0 0 0 0 0] b len=0 cap=5 []
  • 32. Array V.S. Slices func main() { var array = [...]string{"a", "b", "c"} var slice = []string{"a", "b", "c"} fmt.Printf("%Tn", array) fmt.Printf("%Tn", slice) //array = append(array, array...) // Won't compile! slice = append(slice, slice...) fmt.Println(slice) } [3]string []string [a b c a b c]
  • 33. Maps import "fmt" type Vertex struct { Lat, Long float64 } var m map[string]Vertex func main() { m = make(map[string]Vertex) m["Bell Labs"] = Vertex{ 40.68433, -74.39967, } fmt.Println(m["Bell Labs"]) } ● A map maps keys to values. ● Maps must be created with make (not new) before use; the nil map is empty and cannot be assigned to.
  • 34. Maps Literals var m = map[string]Vertex{ "Bell Labs": Vertex{ 40.68433, -74.39967, }, "Google": Vertex{ 37.42202, -122.08408, }, } var m2 = map[string]Vertex{ "Bell Labs": {40.68433, -74.39967}, "Google": {37.42202, -122.08408} } map[key_type]value_type{ key1: value1, key2: value2 … } Can omit type name
  • 35. Mutating Maps m := make(map[string]int) m["Answer"] = 42 Assigning m, ok := m["Answer"] if ok { fmt.Println("key exists!") } Testing if a key exists delete(m, "Answer") Deleting an element
  • 36. Iterating through Slices or Maps package main import "fmt" var pow = []int{1, 2, 4, 8, 16, 32, 64, 128} var m = map[string]int{"a": 1, "b": 2} func main() { for i, v := range pow { fmt.Printf("2**%d = %dn", i, v) } for k, v := range m { fmt.Printf("%s: %dn", k, v) } } Use the range keyword
  • 37. The _ Variable package main import "fmt" var pow = []int{1, 2, 4, 8, 16, 32, 64, 128} var m = map[string]int{"a": 1, "b": 2} func main() { for _, v := range pow { fmt.Printf("%dn", v) } for _, v := range m { fmt.Printf("%dn", v) } } Go won’t let you compile you declare some variable but didn’t use it. In this case, you can assign it to _ to indicate that it will not be used. We don’t use index here, assign it to _. We don’t use key here, assign it to _.
  • 38. Go: Objects without Class ● No class, use interfaces instead ● No inheritance, use embedding instead
  • 39. Method Declaration 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()) } Basically look like functions, but with extra receiver declaration before function name.
  • 40. Method Declaration (cont’d) func (t time.Time) bla() string { return t.LocalTime().String()[0:5] } You may be tempted to declare new method on existing type, like monkey patching in Python or Ruby. cannot define new methods on non-local type time. Time type MyTime time.Time func (t MyTime) bla() string { return t.LocalTime().String()[0:5] } The correct way is to create a type synonym then define method on it.
  • 41. Interfaces type Abser interface { Abs() float64 } type MyFloat float64 func (f MyFloat) Abs() float64 { if f < 0 { return float64(-f) } return float64(f) } ● An interface type is defined by a set of methods. ● A value of interface type can hold any value that implements those methods. ● Interfaces are satisfied implicitly ○ capture idioms and patterns after the code is being written
  • 42. type Vertex struct { X, Y float64 } func (v *Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) } func main() { var a Abser f := MyFloat(-math.Sqrt2) v := Vertex{3, 4} a = f // a MyFloat implements Abser a = &v // a *Vertex implements Abser a = v // a Vertex, does NOT implement Abser fmt.Println(a.Abs()) } Interfaces (cont’d)
  • 43. Interfaces (cont’d) ● Interfaces are infact like static-checked duck typing! ● When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.
  • 44. package main import "fmt" type World struct{} func (w *World) String() string { return "世界" } func main() { fmt.Printf("Hello, %sn", new(World)) } Interfaces: Another Example % go run hello.go Hello, 世界
  • 45. package fmt // Stringer is implemented by any value that has a String method, // which defines the ``native'' format for that value. // The String method is used to print values passed as an operand // to any format that accepts a string or to an unformatted printer // such as Print. type Stringer interface{ String() string } Interfaces: Another Example (cont’d) fmt.Stringer Naming Convention: An interface with single method should be name as method name with “er” as postfix
  • 46. Errors type MyError struct { When time.Time What string } func (e *MyError) Error() string { return fmt.Sprintf("at %v, %s", e.When, e.What) } type error interface { Error() string } Errors in go are defined as interface: Defining new error type
  • 47. Embedding type Car struct { wheelCount int } func (car Car) numberOfWheels() int { return car.wheelCount } type Ferrari struct { Car //anonymous field Car } func main() { f := Ferrari{Car{4}} fmt.Println("A Ferrari has this many wheels: ", f.numberOfWheels()) } Sort of “Inheritance” in OOP.
  • 48. Goroutines func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") } A goroutine is a lightweight thread managed by the Go runtime.
  • 49. Channels ch := make(chan int) ch <- v // Send v to channel ch. v := <-ch // Receive from ch, and // assign value to v. ● Channels are a typed conduit through which you can send and receive values. ● By default, sends and receives block until the other side is ready. ● Channels can be buffered. Provide the buffer length as the second argument to make to initialize a buffered channel: ● Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty. ch := make(chan int, 100)
  • 50. Channels (cont’) func sum(a []int, c chan int) { sum := 0 for _, v := range a { sum += v } c <- sum // send sum to c } func main() { a := []int{7, 2, 8, -9, 4, 0} c := make(chan int) go sum(a[:len(a)/2], c) go sum(a[len(a)/2:], c) x, y := <-c, <-c // receive from c fmt.Println(x, y, x+y) }
  • 51. Closing a Channel close(ch) // close the channel v, ok := <-ch if !ok { fmt.Println("channel closed") } ● A sender can close a channel to indicate that no more values will be sent. ● Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression
  • 52. Select func fibonacci(c, quit chan int) { x, y := 0, 1 for { select { case c <- x: x, y = y, x+y case <-quit: fmt.Println("quit") return } } } The select statement lets a goroutine wait on multiple communication operations.
  • 53. Defer, Panic, and Recover func main() { defer func() { if r := recover(); r != nil { fmt.Println("Recovered in f", r) } }() ofs, err := os.Create("/dev/stdout") if err == nil { fmt.Fprintf(ofs, "Hello, 世界n") } else { panic(err) } defer ofs.Close() } A defer statement pushes a function call onto a list. The list of saved calls is executed after the surrounding function returns.
  • 54. Generics? Why does Go not have generic types? Generics may well be added at some point. We don't feel an urgency for them, although we understand some programmers do. Generics are convenient but they come at a cost in complexity in the type system and run-time. We haven't yet found a design that gives value proportionate to the complexity, although we continue to think about it. Meanwhile, Go's built-in maps and slices, plus the ability to use the empty interface to construct containers (with explicit unboxing) mean in many cases it is possible to write code that does what generics would enable, if less smoothly. From Go website:
  • 55. Type Switch func anything(data interface{}) { switch data.(type) { case int: fmt.Println( "int") case string: fmt.Println( "string") case fmt.Stringer: fmt.Println( "Stringer") default: fmt.Println( "whatever...") } } type World struct{} func (w *World) String() string { return "世界" } func main() { anything("a string") anything(42) anything(new(World)) anything(make([]int, 5)) } string int Stringer whatever...
  • 56. Type Assertion func anything(data interface{}) { s, ok := data.(fmt.Stringer) if ok { fmt.Printf( "We got Stringer: %Tn", s) } else { fmt.Println( "bah!") } } type World struct{} func (w *World) String() string { return "世界" } func main() { anything("a string") anything(42) anything(new(World)) anything(make([]int, 5)) } bah! bah! We got Stringer: *main.World bah!
  • 57. Conclusion ● Go is a fast, small, easy-to-learn language ● Actively developed by Google ● Tons of third-party libraries ● Learn it by using it!
  • 58. References ● Go Official Tutorial [website] ● Effective Go [website] ● Advanced Go Concurrency Patterns [Slide, Video] ● Go and Rust — objects without class [lwn.net]