Golang的short variable declaration

Go中,常规声明方式为

var i, j int

也有一种短的方式

i, j := 1, 2

这种方式相当于

var i, j int
i, j = 1, 2

但是短的方式允许重复声明,条件是必须有一个以上重复

i, j := 1, 2
z, j := 3, 4
i, j := 5, 6 // 报错!

我们来看一段代码:

package main

import (
	"fmt"
	"os"
)

var cwd string

func init() {
	cwd, err := os.Getwd()
	if err != nil {
		fmt.Printf("error = %s\n", err)
	}

	fmt.Printf("init: cwd = %s\n", cwd)
}

func main() {
	fmt.Printf("main: cwd = %s\n", cwd)
}

运行结果:

jiajun@debian $ go run fun.go
init: cwd = /home/jiajun/test
main: cwd =

为什么cwd明明已经声明成了全局变量却没有被没改变呢?

https://golang.org/ref/spec#Short_variable_declarations

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new.

所以上面的代码想要能正常运行就得:

package main

import (
	"fmt"
	"os"
)

var cwd string

func init() {
    var err error
	cwd, err = os.Getwd()
	if err != nil {
		fmt.Printf("error = %s\n", err)
	}

	fmt.Printf("init: cwd = %s\n", cwd)
}

func main() {
	fmt.Printf("main: cwd = %s\n", cwd)
}

更多文章
  • Redis源码阅读:RDB是怎么实现的
  • Redis源码阅读:AOF重写
  • Redis源码阅读:AOF持久化
  • Redis源码阅读:key是怎么过期的
  • Redis源码阅读:字典是怎么实现的
  • Redis源码阅读:执行命令
  • Redis源码阅读:启动过程
  • WAL(Write-ahead logging)的套路
  • 搞定CORS问题
  • 如何定位程序问题所在
  • 设计一个IM归档系统
  • logrotate read only filesystem问题
  • Golang GIN写单测时,愉快的使用返回值
  • Python Queue源码分析
  • Go里优雅的使用全局配置