安装go1.8
No breaking changes
golang信守承诺,不管你是从1.0开始的老用户还是1.7开始的新朋友,放心,你们的代码还能用。(swift程序员哭晕在厕所)
语言层面的修改
作为一门强类型语言中的强类型语言,一个连int和int64都不能隐式类型转换的语言,golang1.8偷偷做出了一些让步。golang1.8中,如果两个结构体的field名称和类型完全一样,只是tag不一样的话,它们之间可以相互convert了。当然,是显式的。
func example() {
type T1 struct {
X int `json:"foo"`
}
type T2 struct {
X int `json:"bar"`
}
var v1 T1
var v2 T2
v1 = T1(v2) // now legal
}
工具链
- 会有一个默认的GOPATH了,默认为$HOME/go
- go get在-insecure这个flag存在时也会走代理了(论走代理的重要性)
- 多了个go bug命令,用于在github上报bug,并自动填好系统信息啥的。
- go doc命令产生的doc会更加有可读性
- 增加了一个plugin package,不过目前只能用于linux,据说是用于动态增加插件的,还没有试用过
运行时与性能
优化了gc机制。
优化了对并发时防止多个goroutine同时读写一个Map的检查。
gc pauses明显缩短,(usually under 100 microseconds and often as low as 10 microseconds. )
编译速度据说快了20%到30%
标准库
上面这些release note告诉我们go1.8比1.7好,但是对于写代码的我们来说,好像也没有什么感觉,反倒是标准库的变化,对我们的影响会比较大。
sort
从前的排序是这么写的
type People struct {
Name string
Age int
}
type Peoples []People
func (peoples Peoples) Len() int {
return len(peoples)
}
// Swap for sort
func (peoples Peoples) Swap(i,j int){
peoples[i], peoples[j] = peoples[j], peoples[i]
}
// Less for sort
func (peoples Peoples) Less(i,j int) bool{
return peoples[i].Age < peoples[j].Age
}
func main() {
people := Peoples{
{"Gopher", 7},
{"Alice", 55},
{"Vera", 24},
{"Bob", 75},
}
sort.Sort(people)
fmt.Println(people)
}
现在,sort包里面有了一个Slice函数,你以后可以这样写了。
// 按名字排序
sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })
// 按年龄排序
sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })
HTTP/2 push
package main
import (
"fmt"
"log"
"net/http"
)
const mainJS = `console.log("hello world");`
const indexHTML = `<html>
<head>
<title>Hello</title>
<script src="/main.js"></script>
</head>
<body>
</body>
</html>
`
func main() {
http.HandleFunc("/main.js", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, mainJS)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
pusher, ok := w.(http.Pusher)
if ok { // Push is supported. Try pushing rather than waiting for the browser.
if err := pusher.Push("/main.js", nil); err != nil {
log.Printf("Failed to push: %v", err)
}
}
fmt.Fprintf(w, indexHTML)
})
log.Fatal(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil))
}
打开命令行,进入项目目录,首先运行
go run $GOROOT/src/crypto/tls/generate_cert.go --host 127.0.0.1
运行结果生成cert.pem和key.pem,然后运行
go run main.go
打开chrome,输入
https://localhost:8080/
打开开发者工具,可以看到main.js是被服务端主动push过来的,而不是浏览器主动发送的get请求。
支持http server graceful shutdown
调用server.Close()的话服务器会直接关闭,而调用server.Shutdown()的话服务器会处理完毕所有已有的连接然后再关闭。
期待
比期待更期待
大神最后给我们灌了一大碗鸡汤,并表示,他觉得虽然golang的泛型在2017年不会发生,但是他不会停止探索在golang上设计泛型。
"When I first started thinking about generics for Go in 2008, the main examples to learn from were C#, Java, Haskell, and ML. None of the approaches in those languages seemed like a perfect fit for Go. Today, there are newer attempts to learn from as well, including Dart, Midori, Rust, and Swift.
It’s been a few years since we ventured out and explored the design space. It is probably time to look around again, especially in light of the insight about mutability and the additional examples set by newer languages. I don’t think generics will happen this year, but I’d like to be able to say I understand the solution space better."