AirJD 焦点
AirJD

没有录音文件
00:00/00:00
加收藏

Go语言入门

发布者 golang
发布于 1431064912831  浏览 8441 关键词 Go 
分享到

第1页

Go语⾔言⼊入门



Sparkle



第2页

⼤大纲

1、什么是Go

 2、为什么我们要学习(使用)Go

 3、语法特性

 4、开始使用

 5、缺点



第3页

1、什么是Go



第4页

什么是Go

Google于2009发布

 TIOBE 2009年度语⾔言

 目前TIOBE排名50以外(吐槽)

 系统级语⾔言

 并发语⾔言



第5页

ECUG

Erlang China User Group

 Effective Cloud User Group

 ECUG Con 2011,国内第⼀一场以 Go 语⾔言为主题 的盛宴!



第6页

CERL 是我写的⼀一个 C++ 程序库,取意于:Erlang For C/C++。但最终其实 CERL 不是⼀一个编程范式类似于 Erlang 的东西,⽽而更象是⼀一个雏形版本的 Golang。



——许式伟



第7页

我发现我花了四年时间锤炼自⼰己用 C 语⾔言构建系统的

能⼒力,试图找到⼀一个规范,可以更好的编写软件。结果 发现只是对 Go 的模仿。

 ——云风



第8页

目前状态

Go1已经发布(第⼀一个稳定版)



第9页

2、为什么我们要学习 (使用)Go



第10页

只用⼀一种语⾔言的攻城师不 是好程序猿



第11页

为什么是Go

多年Java经验

 熟悉Python、Ruby、Scala

 但觉得C/C++困难

 Go语⾔言让我有机会接近系统



第12页

Go特性

源自C(不是C++)

 编译速度

 gc

 特殊的面向对象

 简洁又灵活的语法

 并发



第13页

3、语法特性



第14页

Hello, world!

package main

import "fmt"

func main() { fmt.Println("Hello, world!")

}



第15页

defer

func main() { file := os.Open("hello.txt") defer file.Close()

// use file }



第16页

错误

func main() { file, err := os.Open("hello.txt") if err != nil { return } defer file.Close()

// use file }



第17页

异常

func main() { defer func() { if r := recover(); r != nil { fmt.Println("recover", r) } }()

someFunc() }

func someFunc() { panic("[fail]")

}



第18页

没有类

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())

}



第19页

没有继承

type Egg struct { weight int

}

type Animal struct { weight int

}

type Bird struct { Animal egg Egg

}



第20页

接⼝口

type Abser interface { Abs() float64

}

type MyFloat float64

func (f MyFloat) Abs() float64 { if f < 0 { return float64(-f) } return float64(f)

}



第21页

并发



第22页

为什么不用锁模型

锁的粒度不能太⼤大

 死锁问题

 编程难度

 面对越来越多的CPU



第23页

Scala介绍中提到



第24页

类线程⾏行为

func main() { go myFunc()

go func() { // code

}() }



第25页

类Actor⾏行为

var ch = make(chan int)

func myActor() { data := <-ch // use data

}

func main() { go myActor()

ch <- 1 }



第26页

纤程

同步代码异步效果

 非抢占式

 单线程实现

 ⽆无线程切换,⽆无锁

 理论上可以⽆无限个(内存限制)



第27页

类纤程⾏行为

func myActor() { data := <-ch

// some code

time.Sleep(100) other := <-otherCh

// use data }



第28页

4、开始使用



第30页

安装(通过源码)

$ hg clone -u release https://code.google.com/p/go

 $ cd go/src

 $ ./all.bash



第31页

安装

http://code.google.com/p/go/downloads/list 有Linux Mac FreeBSD Windows的⼆二进制版

 brew install go homebrew for Mac

 GAE for Go



第32页

IDE

暂⽆无完整IDE



第33页

可以不需要IDE,因为Go 已经内置

构建

 包管理

 代码格式化

 通过gocode提供代码提示



第34页

编辑器

Emacs

 Vim

 Sublime

 甚⾄至Notepad



第35页

Sublime+GoSublime +gocode

通过Go命令进⾏行包管理和构建

 语法加亮

 代码提示

 保存时自动格式化代码



第37页

Go命令

go get 安装第三⽅方类库

 go build 构建

 go run hello.go 编译并且运⾏行



第38页

GAE for Go

内置Go,直接使用

 保存自动编译

 不支持Windows

 直接开始Web开发



第39页

学习资源

http://tour.golang.org/

 http://golang.org/doc/

 http://code.google.com/p/golang-china/

 http://www.mikespook.com/

 https://github.com/wonderfo/wonderfogo/wiki

 许式伟《Go语⾔言编程》



第40页

5、缺点



第41页

曾经的缺点

构建⼯工具

 IDE

 windows支持



第42页

缺点

第三⽅方类库

 gc

 多CPU支持

 动态链接



第43页

谢谢

weibo&twitter: sparklezeng

 email: popeast@gmail.com

 website: http://weavesky.com



支持文件格式:*.pdf
上传最后阶段需要进行在线转换,可能需要1~2分钟,请耐心等待。