[英]Can not use same variable name in different go file
Here is my first go file:这是我的第一个 go 文件:
package main
import (
"bufio"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"os"
"strconv"
)
var db13 *sql.DB
then, I create the second go file:然后,我创建第二个 go 文件:
package main
import "database/sql"
var db13 *sql.DB
I got an error saying that: ''db13' redeclared in this package'我收到一条错误消息:“在此包中重新声明了 ''db13'”
Am I miss anything here?我在这里想念什么吗?
both file are in "package main" so if you think about it like they are in the namespace这两个文件都在“package main”中,所以如果您考虑它们就像它们在命名空间中一样
They are in same package thus it is not allowed.它们在同一个 package 中,因此是不允许的。 Also different packages in same directory are not allowed.同一目录下的不同包也是不允许的。
db13
declared in first.go
can be accessed and used in second.go
.在second.go
中声明的db13
可以在first.go
中访问和使用。 No need to declare it again.无需再次声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.