昨晚清理了自己的pocket,发现有好多收藏的文章都没有时间读,挑了挑,选了golang1.8的release notes和ross cox的新年计划读了一下。写下这篇水文以作记录。
安装go1.8
建议使用gvm安装,可以管理多个版本的golang。
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
不了解push技术的同学可以看看wiki
注意,push技术不是用来从服务端发送消息通知到前端的(那是websocket干的)。push一般是将原本需要前端发送一个请求来获取的资源(脚本,css文件,字体文件,图片等等)改成由服务端主动发送。push带来的好处也很明显(减少请求,可以指定push顺序等等)
下面我们来试用一下go1.8里面的push。
创建项目http2push,并创建文件main.go
package main
import (
"fmt"
"log"
"net/http"
)
const mainJS = `console.log("hello world");`
const indexHTML = `
Hello
`
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()的话服务器会处理完毕所有已有的连接然后再关闭。
期待
ross cox的目标
Russ Cox说2017年要搞golang的包管理,
"In particular, other language ecosystems have really raised the bar for what people expect from package management, and the open source world has mostly agreed on semantic versioning, which provides a useful base for inferring version compatibility. "
不禁眼眶一湿。
比期待更期待
泛型泛型泛型。。。泛型有了,map,reduce啥的还会远吗。ross大神说:
"Nothing sparks more heated arguments among Go and non-Go developers than the question of whether Go should have support for generics (or how many years ago that should have happened). I don’t believe the Go team has ever said “Go does not need generics.” What we have said is that there are higher-priority issues facing Go. For example, I believe that better support for package management would have a much larger immediate positive impact on most Go developers than adding generics. But we do certainly understand that for a certain subset of Go use cases, the lack of parametric polymorphism is a significant hindrance."
大神最后给我们灌了一大碗鸡汤,并表示,他觉得虽然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."