1. Go语言简介
Go(又称 Golang)是 Google 的 Robert Griesemer,Rob Pike 及 Ken Thompson 开发的一种静态强类型、编译型语言。Go 语言语法与 C 相近,但功能上有:内存安全,GC(垃圾回收),结构形态及 CSP-style 并发计算。
本文,我们开始学习Golang,主要参考:
2. 开发环境
1、访问Go官网,下载安装Golang环境。
2、把Golang的bin目录添加到Path环境变量。
3、访问Jetbrains-Go,下载安装Goland。
4、在Goland中配置好GOROOT和GOPATH。
3. helloworld
按照国际惯例,学习新语言,先来一个helloworld。新建test.go文件,内容为:
1 2 3 4 5 6 7
| package main
import "fmt"
func main() { fmt.Println("Hello, World!") }
|
Shift+右键打开Powershell,使用go run test.go
,运行代码。
除了使用go run命令之外,还可以使用go build命令,先对代码进行编译,然后运行生成的二进制文件。
由上面这段代码可以看出,Go语言代码的基础组成有包声明、引入包、函数、语句和表达式。除此之外,还有注释、常量、变量、数组、结构体等。
4. Go基础语法
Go的语法和C很像,这里和C进行对比,指出不同之处,方便记忆。
{
不能单独放在一行- 表达式不需要分号结尾
- 变量声明赋值,
var age int = 0
- 数据类型有差异
- 强制类型转换时括号的位置不同
- 支持多变量赋值,按顺序赋值
:=
操作符也可以声明变量,但是只能在函数体内- const声明可以使用iota
- if和for后面不需要圆括号
- switch不需要break
- 循环都用for,没有while
- 函数定义,
func func_name([params]) [return_types]{}
- 数组声明,
var arr [10] float32; var arr2 = [3]float32{1.0, 2.0, 3.4}
- 结构体定义,type先行,下面有demo
- 切片(动态数组),
var slice1 []int = make([]int, 3, 9)
- 切片append,copy
- 字典map定义,
var map1 map[string]string = make(map[string]string)
- 字典delete条目
- for range循环切片和字典
- 定义接口,下面有demo
- 错误处理,error接口,下面有demo
- 并发,go语句开启新线程,下面有demo
结构体demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package main
import "fmt"
type Books struct { title string author string subject string book_id int }
func main() {
fmt.Println(Books{"Go 语言", "www.voidking.com", "Go 语言教程", 6495407})
fmt.Println(Books{title: "Go 语言", author: "www.voidking.com", subject: "Go 语言教程", book_id: 6495407})
fmt.Println(Books{title: "Go 语言", author: "www.voidking.com"})
var Book1 Books
Book1.title = "Go语言入门篇" Book1.author = "www.voidking.com" Book1.subject = "Go语言" Book1.book_id = 123456
printBook(Book1) }
func printBook( book Books ) { fmt.Printf( "Book title : %s\n", book.title); fmt.Printf( "Book author : %s\n", book.author); fmt.Printf( "Book subject : %s\n", book.subject); fmt.Printf( "Book book_id : %d\n", book.book_id); }
|
接口demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package main
import ( "fmt" )
type Phone interface { call() }
type NokiaPhone struct { }
func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!") }
type IPhone struct { }
func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!") }
func main() { var phone Phone
phone = new(NokiaPhone) phone.call()
phone = new(IPhone) phone.call()
}
|
错误处理demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package main
import ( "fmt" )
type DivideError struct { dividee int divider int }
func (de *DivideError) Error() string { strFormat := ` Cannot proceed, the divider is zero. dividee: %d divider: 0 ` return fmt.Sprintf(strFormat, de.dividee) }
func Divide(varDividee int, varDivider int) (result int, errorMsg string) { if varDivider == 0 { dData := DivideError{ dividee: varDividee, divider: varDivider, } errorMsg = dData.Error() return } else { return varDividee / varDivider, "" }
}
func main() { if result, errorMsg := Divide(100, 10); errorMsg == "" { fmt.Println("100/10 = ", result) } if _, errorMsg := Divide(100, 0); errorMsg != "" { fmt.Println("errorMsg is: ", errorMsg) } }
|
并发demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package main
import ( "fmt" "time" )
func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } }
func main() { say("hello") go say("world") }
|
5. 后记
Go语言入门篇,核心就是把Go语言当成C来写,和C不同的地方,留心注意即可。更多高阶的内容,用到的时候再去学习整理。