emacs 开发环境
spacemacs已经集成了不少功能,但是缺少代码提示。因此还需要gocode来辅助。
安装所需的命令
go get -u -v github.com/nsf/gocode
go get -u -v github.com/rogpeppe/godef go get -u -v
golang.org/x/tools/cmd/guru go get -u -v
golang.org/x/tools/cmd/gorename go get -u -v
golang.org/x/tools/cmd/goimports配置文件
将gocode中的
go-autocomplete.el
拷贝至elpa/go-mode
下
向space macs手动添加包:dotspacemacs-additional-packages '(go-autocomplete)
在dotspacemace/user-config
中添加以下内容123(require 'go-autocomplete)(require 'auto-complete-config)(ac-config-default)
GO
语法
声明 :
var|const|type|func name (类型) (值)
var
显式声明一个变元。var name Type
:=
语法可以隐式地声明一个变元。name := f() | a
隐式声明的变元的作用域是可以被覆盖的,但显式的不能。12345var a int //1.会导致3出错a := 1 //2.不会导致3处出错{a:= 2 //3.}const a=2
,const a float64=2
type name define
,define= Type | struct{..}|interface{}
func (name Type)* name (arg...) {...}
打*号的部分是 接收器(receiver),用于扩展指定Type
(必须是自定义类型)。1234567891011type Point struct{ X, Y float64 }// traditional functionfunc (p Point) Distance(q Point) float64 {return math.Hypot(q.X-p.X, q.Y-p.Y)}type Float64 float64 //func (p Float64) Distance(q Float64) float64 {return math.Abs(float64(p - q))}var x, y Float64 = 1.0, 2x.Distance(y)匿名函数:
func(r rune) rune { return r + 1 }
便捷操作1234567891011import ("C""fmt""math")const | var (AbsoluteZeroC Celsius = -273.15 // 绝对零度FreezingC Celsius = 0 // 结冰点温度BoilingC Celsius = 100 // 沸水温度)var a,b,c Type控制结构
if语句12345678if a, b := 21, 3; a > b {fmt.Println("a>b ? true")}else {}for i, j := 1, 10; i < j; i,j=i+1,j+1 { //死循环fmt.Println(i)}switch语句
123456789101112switch ch {case '0':fallthrough //必须是最后一个语句case '1':cl = "Int"case 'A':case 'a':fallthroughcl = "ABC" //errordefault:cl = "Other Char"}类型转换
语法:123456789101112<目标类型> ( <表达式> )<目标类型的值>,<布尔参数> := <表达式>.( 目标类型 ) // 安全类型断言<目标类型的值> := <表达式>.( 目标类型 ) //非安全类型断言var3 := int64(var1)var i interface{} = "TT"j, b := i.(int)if b {fmt.Printf("%T->%d\n", j, j)} else {fmt.Println("类型不匹配")}
基本类型
数组
12345q := [...]int{1, 2, 3}q := [3]int{1, 2, 3} //等价fmt.Printf("%T\n", q) // "[3]int"r := [...]int{99: -1}// {下表:值},填充默认值Slice
Slice(切片)代表变长的序列,序列中每个元素都有相同的类型。一个slice类型一般写作[]T
,其中T代表slice中元素的类型;数组和slice之间有着紧密的联系。一个slice是一个轻量级的数据结构,提供了访问数组子序列(或者全部)元素的功能,而且slice的底层确实引用一个数组对象。一个slice由三个部分构成:指针、长度和容量。array[i:j]
来创建一个Slice,0 ≤ i≤ j≤ cap(s)123456array := [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}front := array[0:5]mid := array[3:8]tail := array[4:9]front[4] = 555 //3个数都被修改array[5] = 605 //会导致 mid,tail 修改注意
12345678910array := [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9}front := array[0:5]mid := array[3:8]tail := array[4:9]nf := append(tail, 66) //1.nf := append(mid, 66) //2.nf[0] = 5555fmt.Println(front, mid, tail, nf)//1. [1 2 3 4 601] [4 601 6 7 8] [601 6 7 8 9] [5555 6 7 8 9 66]//2. [1 2 3 5555 5] [5555 5 6 7 8] [5 6 7 8 66] [5555 5 6 7 8 66]当原数组够用时,append会直接使用原来的空间。不够时另开一片。
[]T
是切片类型[n]T
是数组类型Map
我们也可以用map字面值的语法创建map,同时还可以指定一些最初的key/value:12345ages := make(map[string]int) // mapping from strings to intsages := map[string]int{"alice": 31,"charlie": 34,}结构体
1234567891011type Employee struct {ID intName stringAddress stringDoB time.TimePosition stringSalary intManagerID int}var dilbert Employee接口
一类有相同方法的对象。1234567891011121314package iotype Reader interface {Read(p []byte) (n int, err error)}type Closer interface {Close() error}type ReadWriter interface {Read(p []byte) (n int, err error)Writer}var w io.Writerw = os.Stdout //使用接口
符号
|
|
defer :延迟执行
12defer println("p1") //后被打印defer println("p2") //先被打印假设defer处声明了一个变量,那么在析构的时候执行内容。
range :配合for使用
1234for index, value := range mySlice {fmt.Println("index: " + index)fmt.Println("value: " + value)}for index,char := range string {}
for index,value := range array {}
for index,value := range slice {}
for key,value := range map {}
抽象
不同于面向对象的语言先定义一个对象具有哪些抽象行为再实现的思路。
GO是先实现一个对象,再检查这个对象是否符合接口规范。
方法
扩展某一个类型1234567891011type Point struct{ X, Y float64 }// traditional functionfunc Distance(p, q Point) float64 {return math.Hypot(q.X-p.X, q.Y-p.Y)}// same thing, but as a method of the Point typefunc (p Point) Distance(q Point) float64 {return math.Hypot(q.X-p.X, q.Y-p.Y)}接口
一类有相同方法的对象。1234567891011package iotype Reader interface {Read(p []byte) (n int, err error)}type Closer interface {Close() error}type ReadWriter interface {Read(p []byte) (n int, err error)Writer}
并行
goroutine
使用go关键字go12f() // call f(); wait for it to returngo f() // create a new goroutine that calls f(); don't waitchannel
1234567var ch chan intch = make(chan int) // unbuffered channelch = make(chan int, 0) // unbuffered channelch = make(chan int, 3) // buffered channel with capacity 3ch <- 2 //senda := ch //receivechan<- int
表示一个只发送int的channel,只能发送不能接收。<-chan int
表示一个只接收int的channel,只能接收不能发送。
一个基于无缓存Channels的发送操作将导致发送者goroutine阻塞,直到另一个goroutine在相同的Channels上执行接收操作,当发送的值通过Channels成功传输之后,两个goroutine可以继续执行后面的语句。select
123456789101112select {case <-ch1:// ...case x := <-ch2:// ...use x...case ch3 <- y:// ...case <-time.After(10 * time.Second)://超时机制,不能与default一起default:// ...}
调用C库
|
|
可以添加一下编译选项:
CFLAGS, CPPFLAGS, CXXFLAGS, FFLAGS , LDFLAGS
c代码必须加以注释且后面紧跟import "C"
在Go中使用C的类型
C.char C.schar (signed char) C.uchar (unsigned char) C.short C.ushort (unsigned short) C.int C.uint (unsigned int) C.long C.ulong (unsigned long) C.longlong (long long) C.ulonglong (unsigned long long) C.float C.double C.complexfloat (complex float) C.complexdouble (complex double)
如果是 struct, union, or enum
类型的,会添加前缀 struct_, union_, or enum_
。
如struct。a{...};
在go中变为C.struct_a
。C.sizeof_T
表示C中某种类型的长度
|
|
在go中不能调用c中可变参数的函数。