在Go语言中,使用:=会导致未使用的错误,但使用=则不会。

huangapple go评论92阅读模式
英文:

using := gives unused error but using = don't in Go

问题

我有一段代码,在使用:=时出现错误,但是当我使用=时,它可以正常编译。根据我的了解,:=只需要至少定义一个变量,其他变量不需要定义,但是考虑到这段代码,这是Go语言的一个错误吗?

无法编译的代码:

错误:services/db_service.go:16: Session声明但未使用

package services

import (
	"gopkg.in/mgo.v2"
	"log"
)

const DB = "mmdb_dev"

var Session *mgo.Session

func InitMongo() bool {
	url := "mongodb://localhost"
	log.Println("Establishing MongoDB connection...")
	//var err error
	Session, err := mgo.Dial(url)
	if err != nil {
		log.Fatal("Cannot connect to MongoDB!")
		return true
	} else {
		return false
	}
}

func GetNewSession() mgo.Session {
	return *Session.Copy()
}

编译通过的代码:

package services

import (
	"gopkg.in/mgo.v2"
	"log"
)

const DB = "mmdb_dev"

var Session *mgo.Session

func InitMongo() bool {
	url := "mongodb://localhost"
	log.Println("Establishing MongoDB connection...")
	var err error
	Session, err = mgo.Dial(url)
	if err != nil {
		log.Fatal("Cannot connect to MongoDB!")
		return true
	} else {
		return false
	}
}

func GetNewSession() mgo.Session {
	return *Session.Copy()
}

更改的部分是:

Session, err := mgo.Dial(url)

改为:

var err error
Session, err = mgo.Dial(url)
英文:

I have piece of code in which I get error when I use := but when I use = it compiles properly. What I learned is that := only requires only atleast one variable to be defined, others need not be defined, but considering this code is it a bug in Go?

Uncompilable code:

Error: services/db_service.go:16: Session declared and not used

package services

import (
	"gopkg.in/mgo.v2"
	"log"
)

const DB = "mmdb_dev"

var Session *mgo.Session

func InitMongo() bool {
	url := "mongodb://localhost"
	log.Println("Establishing MongoDB connection...")
	//var err error
	Session, err := mgo.Dial(url)
	if err != nil {
		log.Fatal("Cannot connect to MongoDB!")
		return true
	} else {
		return false
	}
}

func GetNewSession() mgo.Session {
	return *Session.Copy()
}

Compiled code

package services

import (
	"gopkg.in/mgo.v2"
	"log"
)

const DB = "mmdb_dev"

var Session *mgo.Session

func InitMongo() bool {
	url := "mongodb://localhost"
	log.Println("Establishing MongoDB connection...")
	var err error
	Session, err = mgo.Dial(url)
	if err != nil {
		log.Fatal("Cannot connect to MongoDB!")
		return true
	} else {
		return false
	}
}

func GetNewSession() mgo.Session {
	return *Session.Copy()
}

The change is

Session, err := mgo.Dial(url) 

to

var err error
Session, err = mgo.Dial(url)

答案1

得分: 4

运算符:=用于短变量声明。它声明并初始化变量。

在你的第一个例子中,你在全局作用域中声明了Session变量,并且在main函数中你声明了一个同名的新变量在主作用域中(因为你使用了:=运算符)。因此,全局作用域中声明的Session变量未使用,因此出现错误。

在你的第二个例子中,你使用赋值运算符=给全局变量赋值,因此它不是声明一个新的Session变量,而是给现有的全局变量赋值。

请参考一个示例,展示全局变量和局部变量之间的区别。

英文:

The operator := is used for short variable declaration. It declares and initializes the variable.

In your first example, you have declared Session variable in global scope and in main function you've declared a new variable having same name in the main scope (as you have used := operator). Therefore, the Session variable declared in the global scope is unused and hence the error.

In your second example, you have assigned global variable a value using assignment operator = and hence it is not declaring a new Session variable but assigning a value to existing global variable.

Please find an example showing difference between global and local variable.

答案2

得分: 2

你正在遮蔽你的Session变量。你的第一个例子创建了一个新的Session变量,现在它无法编译,因为另一个变量被声明但未使用。

英文:

You are shadowing your Session variable. Your first example is creating a new Session variable and now it won't compile b/c the other is declared but unused.

答案3

得分: 0

当你使用:=时,变量的定义是在函数内部的。也就是说,变量的作用域发生了变化,从全局变为局部。由于你没有在本地使用变量,所以会出现编译错误。

英文:

When you use := the variable definition is within the function. i.e. the scope of the variable changed, from global to local. And you're not using variable locally, hence the compilation error.

huangapple
  • 本文由 发表于 2017年7月14日 10:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/45093599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定