Golang简介

关于Golang,有这样一段话:

1
2
3
When the three of us [ `Thompson`, `Rob Pike`, and `Robert Griesemer` ] got started, it was pure research.
The three of us got together and decided that we hated `C++`. [laughter] ... [Returning to Go,] we started off with the idea that all three of us had to be talked into every feature in the language, so there was =no extraneous garbage= put into the language for any reason.

Golang的目标之一就是克服C++过于复杂的问题.

设计特点:

  • C++是兼容C的,Golang要替代C++;Golang具备类似C的语法,但有更强的安全机制
  • Golang具备动态语言特性
  • 具有包管理机制
  • …….

但对于我而言,吸引我的地方:

  • 网络功能
  • 并发

Hello,World

代码(helloworld.go):

1
2
3
4
5
6
7
8
9
package main
import "fmt"
// this is a comment
func main() {
fmt.Println("Hello World")
}

直接运行:

1
2
3
$ go run helloworld.go
Hello World

编译运行:

1
2
3
4
5
$ go build -o helloworld helloworld.go
$ ./helloworld
Hello World

类型(Types)

Golang是静态类型语言,因此所有变量都要有类型并且类型不可以改变。

内置的数据类型有:

  • Numbers
  • Strings
  • Booleans(=false=, =true=)

变量

举例:

1
2
3
4
var x string = "Hello World"
var x string
x = "Hello World"

可以不指定类型,而直接初始化, =编译器能够根据初始化的内容来推导出数据类型=:

1
2
3
x := "Hello World"
var x = "Hello World"

支持一次定义多个变量,如:

1
2
3
4
5
var (
a = 5
b = 10
c = 15
)

Arrays, Slices and Maps

数组

定义一个数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
var x [5]int
x[4] = 100
x := [5]float64{ 98, 93, 77, 82, 83 }
x := [5]float64{
98,
93,
77,
82,
83,
}

切片

定义Slice:

1
2
3
4
5
var x []float64
arr := [5]float64{1,2,3,4,5}
x := arr[0:5]

想想python里面的切片就知道了。

映射

定义映射:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var x map[string]int
x["key"] = 10
elements := map[string]string{
"H": "Hydrogen",
"He": "Helium",
"Li": "Lithium",
"Be": "Beryllium",
"B": "Boron",
"C": "Carbon",
"N": "Nitrogen",
"O": "Oxygen",
"F": "Fluorine",
"Ne": "Neon",
}

函数

典型的函数:

1
func main() {}

可以返回多个值:

1
2
3
4
5
6
7
func f() (int, int) {
return 5, 6
}
func main() {
x, y := f()
}

闭包

一个支持闭包的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func makeEvenGenerator() func() uint {
i := uint(0)
return func() (ret uint) {
ret = i
i += 2
return
}
}
func main() {
nextEven := makeEvenGenerator()
fmt.Println(nextEven()) // 0
fmt.Println(nextEven()) // 2
fmt.Println(nextEven()) // 4
}

递归

1
2
3
4
5
6
7
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}

Defer, Panic & Recover

  • =defer= :使得函数等待调用其的函数结束后再执行此函数,如(最后执行second):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package main
    import "fmt"
    func first() {
    fmt.Println("1st")
    }
    func second() {
    fmt.Println("2nd")
    }
    func main() {
    defer second()
    first()
    }
  • =Panic=, =Recover=: 用于触发、获取运行时错误

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main
    import "fmt"
    func main() {
    defer func() {
    str := recover()
    fmt.Println(str)
    }()
    panic("PANIC")
    }

指针

和C语言一样,有 =*= 和 =&= 来代表指针和引用,同时加入 =new= 操作符来创建指针:

1
2
3
4
5
6
7
8
func one(xPtr *int) {
*xPtr = 1
}
func main() {
xPtr := new(int)
one(xPtr)
fmt.Println(*xPtr) // x is 1
}

数据结构和接口(用户自定义类型)

定义新的数据结构,类似:

1
2
3
4
5
type Circle struct {
x float64
y float64
r float64
}

=type= 表示定义新类型, 初始化:

1
2
3
4
var c Circle
c := new(Circle)
c := Circle{x: 0, y: 0, r: 5}
c := Circle{0, 0, 5}

为结构体增加操作方法:

1
2
3
4
5
func (c *Circle) area() float64 {
return math.Pi * c.r*c.r
}
fmt.Println(c.area())

核心内置模块

由于没有什么太多新语法,都是从其他语言挑选部分组合成的。自己快速的浏览了一遍语法后,虽然知道有些地方会坑自己,但还是迅速的开始查看核心内置模块:

  • Strings(=strings=)
  • I/O(=io=, =io/ioutil=)
  • FileSystem(=os=, =path/filepath=)
  • Errors(=errors=)
  • Containers & Sort(=container/list=, =sort=)
  • Hashes & Cryptography(=crypto/sha1=, =hash/crc32=)
  • Servers(=net=)
  • HTTP(=net/http=)
  • RPC(=net/rpc=, =net/rpc/jsonrpc=)
  • Parsing Command Line Arguments(=flag=)
  • Synchronization Primitives(=sync=, =sync/atomic=, =sync.Mutex=)

资料

吴羽舒 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!