1、多值返回
package main

import "fmt"

func vals() (int, int) {
	return 3, 7
}

func main() {
	fmt.Println("Hello World")

	a, b := vals()
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(a + b)

    // 占位符
	_, c := vals()
	fmt.Println(c)
}
2、异常处理
import (
	"errors"
	"fmt"
)

func Sqrt(f float64) (float64, error) {
	if f < 0 {
		return 0, errors.New("math: square root of negative number")
	}
	// 实现
	return 1, nil
}

func main() {
	result, err := Sqrt(-1)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Hello, World! ", result)
}
 3、Map
import "fmt"

func main() {
	var countryCapitalMap map[string]string /*创建集合 */
	countryCapitalMap = make(map[string]string)

	var mapInit = map[string]string{"xiaoli": "湖南", "xiaoliu": "天津"}

	/* map插入key - value对,各个国家对应的首都 */
	countryCapitalMap["France"] = "巴黎"
	countryCapitalMap["Italy"] = "罗马"
	countryCapitalMap["Japan"] = "东京"
	countryCapitalMap["India "] = "新德里"

	/*使用键输出地图值 */
	for country := range countryCapitalMap {
		fmt.Println(country, "首都是", countryCapitalMap[country])
	}
	for t := range mapInit {
		fmt.Println(t, " ---> ", countryCapitalMap[t])
	}

	/*查看元素在集合中是否存在 */
	capital, ok := countryCapitalMap["American"] /*如果确定是真实的,则存在,否则不存在 */
	/*fmt.Println(capital) */
	/*fmt.Println(ok) */
	if ok {
		fmt.Println("American 的首都是", capital)
	} else {
		fmt.Println("American 的首都不存在")
	}
}
4、结构体
import "fmt"

type T struct {
	Name string
}

func (t T) M1() {
	t.Name = "name1"
}

func (t *T) M2() {
	t.Name = "name2"
}

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

	// 传递对象
	t1 := T{"name"}
	fmt.Println("M1调用前:", t1.Name)
	t1.M1()
	fmt.Println("M1调用后:", t1.Name)

	// 传递指针
	fmt.Println("M2调用前:", t1.Name)
	t1.M2()
	fmt.Println("M2调用后:", t1.Name)
}
5、接口interface
import (
	"fmt"
)

// 接口
type Phone interface {
	call()
	surfing()
}

//结构体实现类
type NokiaPhone struct {
}

func (nokiaPhone NokiaPhone) call() {
	fmt.Println("I am Nokia, I can call you!")
}

func (nokiaPhone NokiaPhone) surfing() {
	fmt.Println("I am Nokia, I am surfing on internet!")
}

//结构体实现类
type IPhone struct {
}

func (iPhone IPhone) call() {
	fmt.Println("I am iPhone, I can call you!")
}

func (iPhone IPhone) surfing() {
	fmt.Println("I am iPhone, I am surfing on internet!")
}

func main() {
	var phone Phone

	phone = new(NokiaPhone)
	phone.call()
	phone.surfing()

	phone = new(IPhone)
	phone.call()
	phone.surfing()
}
6、并发
import (
	"fmt"
	"time"
)

func say(s string) {
	for i := 0; i < 5; i++ {
		time.Sleep(100 * time.Millisecond)
		fmt.Println(s)
	}
}

func main() {
	//goroutine 是轻量级线程,goroutine 的调度是由 Golang 运行时进行管理的
	go say("world")
	say("hello")
}
import "fmt"

func sum(s []int, c chan int) {
	sum := 0
	for _, v := range s {
		sum += v
	}
	c <- sum // 把 sum 发送到通道 c
}

func main() {
	s := []int{7, 2, 8, -9, 4, 0}

	c := make(chan int)
	go sum(s[:len(s)/2], c)
	go sum(s[len(s)/2:], c)
	x, y := <-c, <-c // 从通道 c 中接收

	fmt.Println(x, y, x+y)
}
import (
	"fmt"
)

func fibonacci(n int, c chan int) {
	x, y := 0, 1
	for i := 0; i < n; i++ {
		c <- x
		x, y = y, x+y
	}
	close(c)
}

func main() {
	c := make(chan int, 10)
	go fibonacci(cap(c), c)
	// range 函数遍历每个从通道接收到的数据,因为 c 在发送完 10 个
	// 数据之后就关闭了通道,所以这里我们 range 函数在接收到 10 个数据
	// 之后就结束了。如果上面的 c 通道不关闭,那么 range 函数就不
	// 会结束,从而在接收第 11 个数据的时候就阻塞了。
	for i := range c {
		fmt.Println(i)
	}
}
7、源码