聊聊golang1.8的release note

昨晚清理了自己的pocket,发现有好多收藏的文章都没有时间读,挑了挑,选了golang1.8的release notesross 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
}

工具链

  1. 会有一个默认的GOPATH了,默认为$HOME/go
  2. go get在-insecure这个flag存在时也会走代理了(论走代理的重要性)
  3. 多了个go bug命令,用于在github上报bug,并自动填好系统信息啥的。
  4. go doc命令产生的doc会更加有可读性
  5. 增加了一个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 = `<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()的话服务器会处理完毕所有已有的连接然后再关闭。

期待

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."

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容