Come With Golang

38
Session G001:Come With Golang --changzeng-- --20160120---
  • Upload

    -
  • Category

    Internet

  • view

    263
  • download

    0

Transcript of Come With Golang

Page 1: Come With Golang

Session G001:Come With Golang

--changzeng----20160120---

Page 2: Come With Golang

A.G.E.N.D.A.1. History

2. Grammar

3. Concurrence

4. Standard Library & Tool Chain

5. Golang & C

6. Production & OpenSource Projects

7. Reference

Page 3: Come With Golang

Section 1: History• November 10, 2009 : Became a Public Open Source Project • March 28, 2012 : Go 1.0.0 Release• August 18,2015 : Latest Version Go 1.5

Release History

Sponsor

Mascot : Gopher Designed By Renée French Who also design Plan9’s mascot Glenda

“Robert Griesemer, Rob Pike and Ken Thompson started sketching the goals for a new language on the white board on September 21, 2007. ”

--Golang FAQ

Rust1.0 Released on May 15,2015 And Latest version is Rust1.5 (How Frequently)

TEAM PLAN 9

Page 4: Come With Golang

Section 2: GrammarStructure

• Every Go program is made up of packages. And executable should have “main” package• “import” is just like “include” in c/c++ and “import” in python. Import other packages• Package “fmt” has PrintXxx just like “printf” in c/c++ and “print” in python• Function begin with “func” followed by it’s name.• “{” should be the same line with “main()” . Think about linux style and Microsoft• “main” takes no argument like python

Page 5: Come With Golang

Section 2: GrammarVariables & Constants

Type

Namedeclare

variable

Only used in scope C11 “auto”

declare Name Start with 0Auto-Increment

Without typedeclare

multiple assignment

Page 6: Come With Golang

Section 2: GrammarBasic Types

Container Types

PointersGo has pointers. A pointer holds the memory address of a variable.

• Arrays• Slices

• Maps

Test if has

Declares a dictionary

Define and assign

Len is element’s countCap is max size

Page 7: Come With Golang

Section 2: GrammarControl Flow

• If…else…Like , statement

• switch

No while in Golang

• for

default break

no condition means TRUE If..elseif…elseif…else

Page 8: Come With Golang

Section 2: GrammarFunction• ordinary function

• method of struct

• closure

return typeargumentsname

return multiply values

named return values

a method is just a function with a receiver argument

a regular function with no change in functionality

Methods with pointer receivers can modify the value to which the receiver points

Functions are values too. They can be passed around just like other values

Lambda

Page 9: Come With Golang

Section 2: GrammarInterface

An interface type is defined as a set of method signatures.

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--Heim, Michael

Golang’s interface is a Duck Type interface

define a interface

implementation of interface

“i” is a interface variable

Calling a method on an interface value executes the method of the same name on its underlying typeThought about OOP’s Polymorphisn

Page 10: Come With Golang

Section 2: GrammarEmpty Interface & Type Switches

• empty interface

a empty interface is just like a (void *)

• type switches

1. i.(T) T is a type get the exact type a false when failed

2. i.(type) return the type of i.

what the real type a interface holds?

Page 11: Come With Golang

Section 2: Grammardefer

A defer statement defers the execution of a function until the surrounding function returns

defer is a modifier declares a function’s invoking

should be a invoking

Output

invoke releaseB firstthen invoke releaseA

Output

calculate at defer

Page 12: Come With Golang

Section 2: Grammarpanic … recover and error

• panicpanic is just like assert()

• recoverrecover will stop panic’s crash

• try..catch..

if g() panic . defer will be invoked . and recover will get the panic

Go programs express error state with error values. The error type is a built-in interface Which has “Error() string” return strerror()

• error

Page 13: Come With Golang

Section 2: GrammarOOP

• polymorphism• inheritance • encapsulation

Page 14: Come With Golang

Section 3: ConcurrenceCSP

communicating sequential processes (CSP). It is a member of the family of mathematical theories of concurrency known as process algebras, or process calculi, based on message passing via channels.

It is More About a Methodology. Not a Implementation or a Invention. • Goroutine : a carrier of a task• Channel : channels to pass message• Select : tools for channels• sync.Xxx : assist to solve synchronized problems

Go Concurrency Patterns: Pipelines and cancellationIn each stage, the goroutines• receive values from upstream via inbound channels• perform some function on that data, usually producing new values• send values downstream via outbound channels

Page 15: Come With Golang

Section 3: ConcurrenceGoruntine

“go”is just like “pthread_create”“sync.WatiGroup” is like “pthread_join” or “pthread_cond_wait”But goroutine is not a thread or process of system levelIt is like a routine or a light weight thread

Page 16: Come With Golang

Section 3: ConcurrenceChannel

create a read-write channel

close channel’s write end

return a channel

receives values from the channel repeatedly until it is closed

“chan <-” : a sent-to only channel“<- chan” : a read only channel

close will cause <- get false with “ok”

Page 17: Come With Golang

Section 3: ConcurrenceSelect

The select statement lets a goroutine wait on multiple communication operations.

A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.

Page 18: Come With Golang

Section 3: ConcurrenceExample

No more callback, No more context

Page 19: Come With Golang

Section 3: ConcurrenceBenchmark

SNG's IO/CPU Benchmark

The Computer Language Benchmarks Game

Summary• Golang is similar to C in IO and Simple CPU based Scenes situation• Compute-intensive situation shouldn’t be Golang’s advantage

Page 20: Come With Golang

Section 3: ConcurrenceGC

Golang Compile

Golang Runtime

Page 21: Come With Golang

Section 3: ConcurrenceGC

garbage collector is a concurrent, tri-color, mark-sweep collector, an idea first proposed by Dijkstra in 1978

Go is building a garbage collector (GC) not only for 2015 but for 2025 and beyond: A GC that supports today’s software development and scales along with new software and hardware throughout the next decade.

--Golang Blog

Result:

• Not a STW(Stop the World) GC ( <= Golang1.4)• GC in 10ms• 85% less than before (<=Golang1.4)

Principle:• Believe golang’s team. Golang1.5 1.6 and more• Write golang with a Cer mind. Maybe you don’t need string pkg• Write GC friendly codes

Page 22: Come With Golang

Section 4: Standard Library & Tool ChainStandard Library

Page 23: Come With Golang

Section 4: Standard Library & Tool ChainIDE

• vim

• emacs

Page 24: Come With Golang

Section 4: Standard Library & Tool ChainIDE

• atom • sublime

• eclipse• IntelliJ IDEA• LITE

Page 25: Come With Golang

Section 4: Standard Library & Tool ChainGoDoc

Page 26: Come With Golang

Section 4: Standard Library & Tool ChainGoFmt

Page 27: Come With Golang

Section 4: Standard Library & Tool ChainGoVet

GoLint

Page 28: Come With Golang

Section 4: Standard Library & Tool ChainGoTest

ppt.go

ppt_test.go

2G/sec 0.54ns/count

Page 29: Come With Golang

Section 5: Golang & CInvoke C function from Golang

Page 30: Come With Golang

Section 5: Golang & CWrite C&Go in *.go

Page 31: Come With Golang

Section 5: Golang & CInvoke C funciton from a *.a

Page 32: Come With Golang

Section 5: Golang & C Invoke C funciton from a *.so

Page 33: Come With Golang

Section 5: Golang & C Invoke C funciton from a *.so

Page 34: Come With Golang

Section 6: Production & OpenSource ProjectsProduction

How is Go used at Google?Go at Google

Page 35: Come With Golang

Section 6: Production & OpenSource ProjectsOpenSource Projects

Page 36: Come With Golang

Section 6: Production & OpenSource ProjectsOpenSource Projects

Zookeeper(ZAB/Paxos)

K-V Story And Service Discovery (Raft)

Distributed Messaging Platform (Message Queue)

RabbitMQ/Kafak/ZeroMQ/Nanomsg

Web Framework

Djanngo/Tornado/WebpyJfinal SSHLaravel/Flight/Yii

Microservices Framework

Page 37: Come With Golang

Section 7: Reference

① A Tour of Go② Effective Go③ Package Documentation④ Language Specification⑤ The Go Memory Model⑥ The Go Blog⑦ Go by Example⑧ 《 The Go Programming Language 》⑨ 《 Go In Action 》⑩ 《 Mastering Concurrency in Go 》

Example Code is [HERE]

Page 38: Come With Golang

Q & A …The EndThanks…