简体   繁体   English

不能在不同的 go 文件中使用相同的变量名

[英]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.

相关问题 如何在Go中使用与包名相同的变量名? - How does one use a variable name with the same name as a package in Go? 如何导入和使用不同的同名包 - How to import and use different packages of the same name 从同一文件系统中的不同存储库导入 Go 模块 - Importing Go modules from a different repository in the same file system 生成的go文件的名称 - Name for a generated go file 如何在go中使用os.Name()? - How can I use os.Name() in go? 如何使用go.uber.org/zap lib以不同的日志级别打印不同的颜色,并根据日志级别将日志追加到不同的文件? - How can I use go.uber.org/zap lib to print different color with different log level and append log to different file depend on the log level? 我们在哪里可以在Go中使用可变作用域和阴影? - Where can we use Variable Scoping and Shadowing in Go? 如何从init函数(在main.go内部)将变量导入go中的其他文件? - How to import a variable from an init function (inside main.go) to a different file in go? 如何让 VSCode 为同一仓库中的不同目录使用不同的 Go 环境变量? - How to get VSCode to use different Go env vars for different directories in the same repo? 无法从 GO 中的不同文件导入 package - Can't import a package from a different file in GO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM