SlideShare a Scribd company logo
1 of 22
Go之道
大道至简——LESS CAN BE MORE
有时候少意味着更多,你理解得越深,就能越干练。REMEMBER THAT!
Go的三驾马车
Go的故事
 time.Unix(time.Now().Unix(),0).Format(“2006-01-02
15:04:05”)
 C++ => C++11 => C++委员会真的认为C++的特性还不够多?=> 简化这
门语言必是一门更大的成就!
 我们再也不想用C++了,同时也很希望能在写Google的代码时能用到并行
性特性。“大代码”问题也是一个需要关注的问题
 Go是为大型程序设计的,需要大的团队编写和维护。
 Go的目标是解放程序员!
Go的应用
 Docker
CoreOS
 Kubernetes
 七牛云存储
 奇虎360消息推送
2012年 20台服务器上,2000万实时连接,每天发送200万信息。
现在 400台服务器运行,2亿实时连接,每天发送100亿条信息。
 京东云消息推送系统
(团队人数:4)
单机并发tcp连接数峰值118w
内存占用23G(Res)
Load 0.7左右
心跳包 4k/s
gc时间2-3.x s
Go的前景
 2009年度语言
 “ 2015 will be Go's biggest year yet.”
 Go 是互联网时代的C语言,不仅会制霸云计算,10 年内将会制霸整个 IT 领域。
 OS ARCH OS version
linux 386 / amd64 / arm >= Linux 2.6
darwin 386 / amd64 OS X (Snow Leopard + Lion)
freebsd 386 / amd64 >= FreeBSD 7
windows 386 / amd64 >= Windows 2000
 Android NDK
 IOS 1.5实验性支持
 Windows phone
一、类型
1.变量
 var a int =0  a := 0
 var t int64 = time.Now().Unix()
 var a , b int =0, 1
 var str string = “hello”  str := “hello”
 var a, str = 0,”hello”
 var {
a int = 0
str string=“hello”
}
2.函数 func
 func main(){}
 func Select(handler *Handler , sqlTrain
[]map[string]string) (int , []map[string]string){}
 func main(){
handler := &Handler{}
sqlTrain := make([]map[string]string , 3)
n, m := Select(handler , sqlTrain)
}
3.容器(slice , map)
 slice(切片)
x := make([]T, LEN, CAP)
sqlTrain := make([]map[string]string, 3)
 map
x := make(map[T]T)
m := make(map[string]string)
4.控制结构(if…else,for,range,switch)
 if b {
}else{
}
 if b {
}else if{
}
 for i:=0 ; i < 10; i++ {}
 for i , m : = range sqlTrain{}
 for k , v := range m{}
 switch channel {
case 0:
case 1:
case 2:
case 3:
default:
}
 switch channel {
case 0,1,2:
case 3:
default:
}
 fallthrough
一个类型 constant
两个结构 struct{},interface{}
三个函数 len(),append(),copy()
 func CheckVpl(vpl_number string) {
 const pattern = `^[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵
云藏陕甘青宁新渝][A-Z][A-HJ-NP-Z0-9学挂港澳警练]{5}$`
 matched, err := regexp.MatchString(pattern, vpl_number)
 if err != nil {
 logger.Println(err)
 }
 if !matched {
 panic("INVALID-VPL-CHARACTOR")
 }
 }
 points := [][2]int{ {4, 6}, {}, {-7, 11}, {15, 17}, {14,
-8} }
 for _, point := range points {
fmt.Printf("(%d, %d) ", point[0], point[1])
}
 points := []struct{ x, y int }{ {4, 6}, {}, {-7, 11},
{15, 17}, {14, -8} }
 for _, point := range points {
fmt.Printf("(%d, %d) ", point.x, point.y)
}
anonymous struct(匿名结构体)
 {
 "id": "string",
 "park": {
 "name": "string",
 "address": "string",
 "code": "string"
 },
 "in": {
 "time": "yyyy-MM-dd HH:mm",
 "channel": "string",
 "picture": "string"
 },
 "out": {
 "time": "yyyy-MM-dd HH:mm"
 },
 "number": "string",
 "charge": {
 "due": 10000,
 "unpaid": 300,
 "paid": 0,
 "duration": 1000
 }
 }
embedding struct(嵌入结构体)
 type BillsDataRecordT struct {
 Id string `json:"id"`
 Park struct {
 Name string `json:"name"`
 Address string `json:"address"`
 Code string `json:"code"`
 } `json:"park"`
 In struct {
 Time string `json:"time"`
 Channel string `json:"channel"`
 Picture string `json:"picture"`
 } `json:"in"`
 Out struct {
 Time string `json:"time"`
 } `json:"out"`
 Number string `json:"number"`
 Charge struct {
 Due int64 `json:"due"`
 Unpaid int64 `json:"unpaid"`
 Duration int64 `json:"duration"`
 Paid int64 `json:"paid"`
 } `json:"charge"`
 }
结构体接口
 type M struct {
 m []map[float64]map[string]string
 }
 func (m M) Len() int {
 return len(m.m)
 }
 func (m M) Swap(i, j int) {
 m.m[i], m.m[j] = m.m[j], m.m[i]
 }
 func (m M) Less(i, j int) bool {
 var d1, d2 float64
 for d1, _ = range m.m[i] {
 }
 for d2, _ = range m.m[j] {
 }
 return d1 < d2
 }
interface{}(接口)(void*)
 An interface is a type that can be formally defined by
specifying a particular set of methods.
 Interfaces are abstract and cannot be instantiated.
 A concrete (i.e., noninterface) type that has the
methods specified by an interface fulfills the
interface, that is, values of such a concrete type can
be used as values of the interface’s type as well as of
their own actual type.
 type ParkT struct {
 Id string `json:"id"`
 Park struct {
 Name string `json: "name"`
 Code string `json: "code"`
 Address string `json: "address"`
 Gps string `json: "gps"`
 Type string `json: "type"`
 Open_time string `json: "open_time"`
 Criterion string `json: "criterion"`
 } `json:"park"`
 Distance float64 `json: "distance"`
 Space struct {
 Total int `json: "total"`
 Empty int `json: "empty"`
 } `json:"space"`
 Payment struct {
 Online_pay int `json: "online_pay"`
 Cash int `json: "cash"`
 UnionPay int `json: "unionpay"`
 Alipay int `json: “alipay"`
 Weixin int `json: "weixin"`
 Wallet int `json: “wallet"`
 } `json: "payment"`
 }
len()
 str := “京A00000”
fmt.Printf(“%dn”,len(str))
 sqlTrain := make([]map[string]string,0)
fmt.Printf(“%dn”,len(sqlTrain))
 a :=[…]int{}
 a :=[…]int{1,2,3}
 a :=[3]int{1,2,3}
fmt.Printf(“%dn”,len(a))
 The len built-in function returns the length of v, according to its type:
 Array: the number of elements in v.
 Pointer to array: the number of elements in *v (even if v is nil).
 Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
 String: the number of bytes in v.
 Channel: the number of elements queued (unread) in the channel buffer;
 if v is nil, len(v) is zero.
append()
 The append built-in function appends elements to the end of a slice. If
it has sufficient capacity, the destination is resliced to accommodate the
new elements. If it does not, a new underlying array will be allocated.
 Append returns the updated slice. It is therefore necessary to store the
result of append, often in the variable holding the slice itself:
slice = append(slice, elem1, elem2)
slice = append(slice, anotherSlice...)
 As a special case, it is legal to append a string to a byte slice, like this:
slice = append([]byte("hello "), "world"...)
copy()
 The append built-in function appends elements to the end of a slice. If
it has sufficient capacity, the destination is resliced to accommodate the
new elements. If it does not, a new underlying array will be allocated.
 Append returns the updated slice. It is therefore necessary to store the
result of append, often in the variable holding the slice itself:
slice = append(slice, elem1, elem2)
slice = append(slice, anotherSlice...)
 As a special case, it is legal to append a string to a byte slice, like this: slice =
append([]byte("hello "), "world"...)

More Related Content

What's hot

High performance web programming with C++14
High performance web programming with C++14High performance web programming with C++14
High performance web programming with C++14Matthieu Garrigues
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011Deepak Singh
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with StatixEelco Visser
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileHarjinder Singh
 
Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»SpbDotNet Community
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Languagemspline
 
Why my Go program is slow?
Why my Go program is slow?Why my Go program is slow?
Why my Go program is slow?Inada Naoki
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++Fahim Adil
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrencyxu liwei
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeDmitri Nesteruk
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumlercorehard_by
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersEelco Visser
 

What's hot (20)

Modern C++
Modern C++Modern C++
Modern C++
 
High performance web programming with C++14
High performance web programming with C++14High performance web programming with C++14
High performance web programming with C++14
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
 
C++11
C++11C++11
C++11
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical File
 
Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
C Structure and Union in C
C Structure and Union in CC Structure and Union in C
C Structure and Union in C
 
Why my Go program is slow?
Why my Go program is slow?Why my Go program is slow?
Why my Go program is slow?
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
C++ via C#
C++ via C#C++ via C#
C++ via C#
 
Computer hw1
Computer hw1Computer hw1
Computer hw1
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in CppEffective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
 
C# 7
C# 7C# 7
C# 7
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
 

Viewers also liked

新浪微博大规模基于Docker的混合云应用实践 -王关胜
新浪微博大规模基于Docker的混合云应用实践 -王关胜新浪微博大规模基于Docker的混合云应用实践 -王关胜
新浪微博大规模基于Docker的混合云应用实践 -王关胜Weibo Corporation
 
Succeeding with OpenStack in the Enterprise (OpenStack Summit Austin 2016)
Succeeding with OpenStack in the Enterprise (OpenStack Summit Austin 2016)Succeeding with OpenStack in the Enterprise (OpenStack Summit Austin 2016)
Succeeding with OpenStack in the Enterprise (OpenStack Summit Austin 2016)Omri Gazitt
 
统一的云平台实现IT大集中和核心网云化
统一的云平台实现IT大集中和核心网云化统一的云平台实现IT大集中和核心网云化
统一的云平台实现IT大集中和核心网云化Kun Liu
 
浅谈架构升级
浅谈架构升级浅谈架构升级
浅谈架构升级Hardway Hou
 
量子云:高性能云计算在影视行业应用
量子云:高性能云计算在影视行业应用量子云:高性能云计算在影视行业应用
量子云:高性能云计算在影视行业应用Hardway Hou
 
Virtual Hadoop Introduction In Chinese
Virtual Hadoop Introduction In ChineseVirtual Hadoop Introduction In Chinese
Virtual Hadoop Introduction In Chinese天青 王
 
网易蜂巢容器公有云的docker实践
网易蜂巢容器公有云的docker实践网易蜂巢容器公有云的docker实践
网易蜂巢容器公有云的docker实践正炎 高
 
Using Rally for OpenStack certification at Scale
Using Rally for OpenStack certification at ScaleUsing Rally for OpenStack certification at Scale
Using Rally for OpenStack certification at ScaleBoris Pavlovic
 
企业PaaS助力数字化转型
企业PaaS助力数字化转型企业PaaS助力数字化转型
企业PaaS助力数字化转型Hardway Hou
 
从玩具到生产 - Cloud Foundry 上海峰会2015年
从玩具到生产 - Cloud Foundry 上海峰会2015年从玩具到生产 - Cloud Foundry 上海峰会2015年
从玩具到生产 - Cloud Foundry 上海峰会2015年Duncan Johnston-Watt
 
迎接云计算大时代 - EasyStack 联合创始人兼CTO 刘国辉
迎接云计算大时代 - EasyStack 联合创始人兼CTO 刘国辉迎接云计算大时代 - EasyStack 联合创始人兼CTO 刘国辉
迎接云计算大时代 - EasyStack 联合创始人兼CTO 刘国辉Hardway Hou
 
企业变革时代的云化之路
企业变革时代的云化之路企业变革时代的云化之路
企业变革时代的云化之路Hardway Hou
 
云计算推动金融业 I T 架构变革
云计算推动金融业 I T 架构变革云计算推动金融业 I T 架构变革
云计算推动金融业 I T 架构变革Hardway Hou
 
Cloud Platform for IoT
Cloud Platform for IoTCloud Platform for IoT
Cloud Platform for IoTNaoto Umemori
 
Architecting your Cloud Strategy - Part One.vsdx
Architecting your Cloud Strategy - Part One.vsdxArchitecting your Cloud Strategy - Part One.vsdx
Architecting your Cloud Strategy - Part One.vsdxGareth Llewellyn
 
Patterns of Expertise in Cloud 云计算中的专家模式 QCon 2014 北京
Patterns of Expertise in Cloud 云计算中的专家模式 QCon 2014 北京Patterns of Expertise in Cloud 云计算中的专家模式 QCon 2014 北京
Patterns of Expertise in Cloud 云计算中的专家模式 QCon 2014 北京Li Yi
 
Docker home ted
Docker home tedDocker home ted
Docker home tedLayne Peng
 
Should I move my database to the cloud?
Should I move my database to the cloud?Should I move my database to the cloud?
Should I move my database to the cloud?James Serra
 

Viewers also liked (18)

新浪微博大规模基于Docker的混合云应用实践 -王关胜
新浪微博大规模基于Docker的混合云应用实践 -王关胜新浪微博大规模基于Docker的混合云应用实践 -王关胜
新浪微博大规模基于Docker的混合云应用实践 -王关胜
 
Succeeding with OpenStack in the Enterprise (OpenStack Summit Austin 2016)
Succeeding with OpenStack in the Enterprise (OpenStack Summit Austin 2016)Succeeding with OpenStack in the Enterprise (OpenStack Summit Austin 2016)
Succeeding with OpenStack in the Enterprise (OpenStack Summit Austin 2016)
 
统一的云平台实现IT大集中和核心网云化
统一的云平台实现IT大集中和核心网云化统一的云平台实现IT大集中和核心网云化
统一的云平台实现IT大集中和核心网云化
 
浅谈架构升级
浅谈架构升级浅谈架构升级
浅谈架构升级
 
量子云:高性能云计算在影视行业应用
量子云:高性能云计算在影视行业应用量子云:高性能云计算在影视行业应用
量子云:高性能云计算在影视行业应用
 
Virtual Hadoop Introduction In Chinese
Virtual Hadoop Introduction In ChineseVirtual Hadoop Introduction In Chinese
Virtual Hadoop Introduction In Chinese
 
网易蜂巢容器公有云的docker实践
网易蜂巢容器公有云的docker实践网易蜂巢容器公有云的docker实践
网易蜂巢容器公有云的docker实践
 
Using Rally for OpenStack certification at Scale
Using Rally for OpenStack certification at ScaleUsing Rally for OpenStack certification at Scale
Using Rally for OpenStack certification at Scale
 
企业PaaS助力数字化转型
企业PaaS助力数字化转型企业PaaS助力数字化转型
企业PaaS助力数字化转型
 
从玩具到生产 - Cloud Foundry 上海峰会2015年
从玩具到生产 - Cloud Foundry 上海峰会2015年从玩具到生产 - Cloud Foundry 上海峰会2015年
从玩具到生产 - Cloud Foundry 上海峰会2015年
 
迎接云计算大时代 - EasyStack 联合创始人兼CTO 刘国辉
迎接云计算大时代 - EasyStack 联合创始人兼CTO 刘国辉迎接云计算大时代 - EasyStack 联合创始人兼CTO 刘国辉
迎接云计算大时代 - EasyStack 联合创始人兼CTO 刘国辉
 
企业变革时代的云化之路
企业变革时代的云化之路企业变革时代的云化之路
企业变革时代的云化之路
 
云计算推动金融业 I T 架构变革
云计算推动金融业 I T 架构变革云计算推动金融业 I T 架构变革
云计算推动金融业 I T 架构变革
 
Cloud Platform for IoT
Cloud Platform for IoTCloud Platform for IoT
Cloud Platform for IoT
 
Architecting your Cloud Strategy - Part One.vsdx
Architecting your Cloud Strategy - Part One.vsdxArchitecting your Cloud Strategy - Part One.vsdx
Architecting your Cloud Strategy - Part One.vsdx
 
Patterns of Expertise in Cloud 云计算中的专家模式 QCon 2014 北京
Patterns of Expertise in Cloud 云计算中的专家模式 QCon 2014 北京Patterns of Expertise in Cloud 云计算中的专家模式 QCon 2014 北京
Patterns of Expertise in Cloud 云计算中的专家模式 QCon 2014 北京
 
Docker home ted
Docker home tedDocker home ted
Docker home ted
 
Should I move my database to the cloud?
Should I move my database to the cloud?Should I move my database to the cloud?
Should I move my database to the cloud?
 

Similar to Go之道

Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Andreas Dewes
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvmArnaud Giuliani
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeMicrosoft Tech Community
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeMicrosoft Tech Community
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePeter Solymos
 
SVGo: a Go Library for SVG generation
SVGo: a Go Library for SVG generationSVGo: a Go Library for SVG generation
SVGo: a Go Library for SVG generationAnthony Starks
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)Sami Said
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesEelco Visser
 

Similar to Go之道 (20)

Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Linked list
Linked listLinked list
Linked list
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the code
 
Getting Input from User
Getting Input from UserGetting Input from User
Getting Input from User
 
Unit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptxUnit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptx
 
SVGo: a Go Library for SVG generation
SVGo: a Go Library for SVG generationSVGo: a Go Library for SVG generation
SVGo: a Go Library for SVG generation
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
 

Go之道

  • 1. Go之道 大道至简——LESS CAN BE MORE 有时候少意味着更多,你理解得越深,就能越干练。REMEMBER THAT!
  • 3. Go的故事  time.Unix(time.Now().Unix(),0).Format(“2006-01-02 15:04:05”)  C++ => C++11 => C++委员会真的认为C++的特性还不够多?=> 简化这 门语言必是一门更大的成就!  我们再也不想用C++了,同时也很希望能在写Google的代码时能用到并行 性特性。“大代码”问题也是一个需要关注的问题  Go是为大型程序设计的,需要大的团队编写和维护。  Go的目标是解放程序员!
  • 4. Go的应用  Docker CoreOS  Kubernetes  七牛云存储  奇虎360消息推送 2012年 20台服务器上,2000万实时连接,每天发送200万信息。 现在 400台服务器运行,2亿实时连接,每天发送100亿条信息。  京东云消息推送系统 (团队人数:4) 单机并发tcp连接数峰值118w 内存占用23G(Res) Load 0.7左右 心跳包 4k/s gc时间2-3.x s
  • 5. Go的前景  2009年度语言  “ 2015 will be Go's biggest year yet.”  Go 是互联网时代的C语言,不仅会制霸云计算,10 年内将会制霸整个 IT 领域。  OS ARCH OS version linux 386 / amd64 / arm >= Linux 2.6 darwin 386 / amd64 OS X (Snow Leopard + Lion) freebsd 386 / amd64 >= FreeBSD 7 windows 386 / amd64 >= Windows 2000  Android NDK  IOS 1.5实验性支持  Windows phone
  • 7. 1.变量  var a int =0  a := 0  var t int64 = time.Now().Unix()  var a , b int =0, 1  var str string = “hello”  str := “hello”  var a, str = 0,”hello”  var { a int = 0 str string=“hello” }
  • 8. 2.函数 func  func main(){}  func Select(handler *Handler , sqlTrain []map[string]string) (int , []map[string]string){}  func main(){ handler := &Handler{} sqlTrain := make([]map[string]string , 3) n, m := Select(handler , sqlTrain) }
  • 9. 3.容器(slice , map)  slice(切片) x := make([]T, LEN, CAP) sqlTrain := make([]map[string]string, 3)  map x := make(map[T]T) m := make(map[string]string)
  • 10. 4.控制结构(if…else,for,range,switch)  if b { }else{ }  if b { }else if{ }  for i:=0 ; i < 10; i++ {}  for i , m : = range sqlTrain{}  for k , v := range m{}
  • 11.  switch channel { case 0: case 1: case 2: case 3: default: }  switch channel { case 0,1,2: case 3: default: }  fallthrough
  • 12. 一个类型 constant 两个结构 struct{},interface{} 三个函数 len(),append(),copy()  func CheckVpl(vpl_number string) {  const pattern = `^[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵 云藏陕甘青宁新渝][A-Z][A-HJ-NP-Z0-9学挂港澳警练]{5}$`  matched, err := regexp.MatchString(pattern, vpl_number)  if err != nil {  logger.Println(err)  }  if !matched {  panic("INVALID-VPL-CHARACTOR")  }  }
  • 13.  points := [][2]int{ {4, 6}, {}, {-7, 11}, {15, 17}, {14, -8} }  for _, point := range points { fmt.Printf("(%d, %d) ", point[0], point[1]) }  points := []struct{ x, y int }{ {4, 6}, {}, {-7, 11}, {15, 17}, {14, -8} }  for _, point := range points { fmt.Printf("(%d, %d) ", point.x, point.y) } anonymous struct(匿名结构体)
  • 14.  {  "id": "string",  "park": {  "name": "string",  "address": "string",  "code": "string"  },  "in": {  "time": "yyyy-MM-dd HH:mm",  "channel": "string",  "picture": "string"  },  "out": {  "time": "yyyy-MM-dd HH:mm"  },  "number": "string",  "charge": {  "due": 10000,  "unpaid": 300,  "paid": 0,  "duration": 1000  }  } embedding struct(嵌入结构体)
  • 15.  type BillsDataRecordT struct {  Id string `json:"id"`  Park struct {  Name string `json:"name"`  Address string `json:"address"`  Code string `json:"code"`  } `json:"park"`  In struct {  Time string `json:"time"`  Channel string `json:"channel"`  Picture string `json:"picture"`  } `json:"in"`  Out struct {  Time string `json:"time"`  } `json:"out"`  Number string `json:"number"`  Charge struct {  Due int64 `json:"due"`  Unpaid int64 `json:"unpaid"`  Duration int64 `json:"duration"`  Paid int64 `json:"paid"`  } `json:"charge"`  }
  • 16. 结构体接口  type M struct {  m []map[float64]map[string]string  }  func (m M) Len() int {  return len(m.m)  }  func (m M) Swap(i, j int) {  m.m[i], m.m[j] = m.m[j], m.m[i]  }  func (m M) Less(i, j int) bool {  var d1, d2 float64  for d1, _ = range m.m[i] {  }  for d2, _ = range m.m[j] {  }  return d1 < d2  }
  • 17. interface{}(接口)(void*)  An interface is a type that can be formally defined by specifying a particular set of methods.  Interfaces are abstract and cannot be instantiated.  A concrete (i.e., noninterface) type that has the methods specified by an interface fulfills the interface, that is, values of such a concrete type can be used as values of the interface’s type as well as of their own actual type.
  • 18.  type ParkT struct {  Id string `json:"id"`  Park struct {  Name string `json: "name"`  Code string `json: "code"`  Address string `json: "address"`  Gps string `json: "gps"`  Type string `json: "type"`  Open_time string `json: "open_time"`  Criterion string `json: "criterion"`  } `json:"park"`  Distance float64 `json: "distance"`  Space struct {  Total int `json: "total"`  Empty int `json: "empty"`  } `json:"space"`  Payment struct {  Online_pay int `json: "online_pay"`  Cash int `json: "cash"`  UnionPay int `json: "unionpay"`  Alipay int `json: “alipay"`  Weixin int `json: "weixin"`  Wallet int `json: “wallet"`  } `json: "payment"`  }
  • 19. len()  str := “京A00000” fmt.Printf(“%dn”,len(str))  sqlTrain := make([]map[string]string,0) fmt.Printf(“%dn”,len(sqlTrain))  a :=[…]int{}  a :=[…]int{1,2,3}  a :=[3]int{1,2,3} fmt.Printf(“%dn”,len(a))
  • 20.  The len built-in function returns the length of v, according to its type:  Array: the number of elements in v.  Pointer to array: the number of elements in *v (even if v is nil).  Slice, or map: the number of elements in v; if v is nil, len(v) is zero.  String: the number of bytes in v.  Channel: the number of elements queued (unread) in the channel buffer;  if v is nil, len(v) is zero.
  • 21. append()  The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated.  Append returns the updated slice. It is therefore necessary to store the result of append, often in the variable holding the slice itself: slice = append(slice, elem1, elem2) slice = append(slice, anotherSlice...)  As a special case, it is legal to append a string to a byte slice, like this: slice = append([]byte("hello "), "world"...)
  • 22. copy()  The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated.  Append returns the updated slice. It is therefore necessary to store the result of append, often in the variable holding the slice itself: slice = append(slice, elem1, elem2) slice = append(slice, anotherSlice...)  As a special case, it is legal to append a string to a byte slice, like this: slice = append([]byte("hello "), "world"...)