Golang新建文件未被识别的问题如何解决
Golang新建文件未被识别的问题如何解决 我是Go语言的新手,目前遇到一个问题:Go没有自动识别我的新文件。项目结构如下:
parent
|_custompackage
| |_customFile.go
| |_go.mod
|_examplepackage
| |_examplefile1.go
| |_examplefile2.go
| |_go.mod
|_mymain
|_mainfile.go
|_go.mod
以下是各个go.mod文件的内容:
custompackage 的 go.mod:
module username/GoLearn/custompackage
go 1.23.1
examplepackage 的 go.mod:
module username/GoLearn/examplepackage
go 1.23.1
mainfile 的 go.mod:
module username/GoLearn/mainfolder
go 1.23.1
replace username/GoLearn/custompackage => ../custompackage
replace username/GoLearn/examplepackage => ../examplepackage
require (
username/GoLearn/custompackage v0.0.0-00010101000000-000000000000
username/GoLearn/examplepackage v0.0.0-00010101000000-000000000000
)
当我尝试运行包含以下示例内容的mainFile时:
package mainfolder
import cust "username/GoLearn/custompackage"
import custfunc "username/GoLearn/examplepackage"
func main() {
custfunc.examplefile2function() // 这个函数会报错,提示找不到
custfunc.examplefile1function() // 这个函数可以正常找到并执行
cust.customfilefunction() // 这个函数可以正常找到并执行
}
我不太明白为什么Go没有识别我新创建的 examplefile2。到目前为止,我已经尝试过使用以下命令:
go get .
go mod tidy
请问有人能帮我解决这个问题吗?
更多关于Golang新建文件未被识别的问题如何解决的实战教程也可以访问 https://www.itying.com/category-94-b0.html
前往 go.dev 官方网站,Golang 并不算太难,花点时间就能上手,但要深入掌握仍需专门学习。
更多关于Golang新建文件未被识别的问题如何解决的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
这确实解决了我的问题,非常感谢你,Christopher。 开箱即用,Golang 中有关于规则(rules)的文档吗?我是新手,可能经常弄错东西。
正如 @peakedshout 所说,或许可以看看 Go by Example 来获得基于示例的学习方法。
想要获得交互式体验,可以看看 Try Go in Y minutes。
这是从 “Learn Go in Y Minutes” 移植过来的版本,但包含了可以直接在浏览器中运行的代码示例。(你可能会发现此页面与原始页面存在差异。这得怪我。我移植了原始页面,并擅自重构了一些内容并添加了泛型。)
引用 TriNguyen:
custfunc.examplefile2function() //this will be reported as not found custfunc.examplefile1function() //this will be found and can be executed normally cust.customfilefunction() //this will be found and can be executed normally
你好 @TriNguyen,
一般来说,函数必须以大写字母开头,才能被其他包找到:
func Examplefile2function()
func Examplefile1function()
func Customfilefunction()
然后,在 mainfolder 中:
custfunc.Examplefile2function() //this will be reported as not found
custfunc.Examplefile1function() //this will be found and can be executed normally
cust.Customfilefunction() //this will be found and can be executed normally
顺便提一下,包含 func main() 的包必须命名为 package main。否则,Go 编译器无法构建可执行文件。
问题在于Go模块的导出规则。examplefile2.go中的函数没有以大写字母开头,因此无法被其他包导入。Go语言中,只有首字母大写的标识符才是导出的(public),可以被外部包访问。
以下是示例代码说明:
examplefile2.go(问题文件):
package examplepackage
// 这个函数不会被导出(小写开头)
func examplefile2function() {
// 函数实现
}
// 这个函数会被导出(大写开头)
func Examplefile2function() {
// 函数实现
}
mainfile.go(需要相应修改):
package main
import (
cust "username/GoLearn/custompackage"
custfunc "username/GoLearn/examplepackage"
)
func main() {
// 错误:无法访问小写开头的函数
// custfunc.examplefile2function()
// 正确:访问大写开头的函数
custfunc.Examplefile2function()
custfunc.Examplefile1function()
cust.Customfilefunction()
}
同时检查examplefile1.go和customFile.go中的函数定义,确保它们也是以大写字母开头的:
examplefile1.go:
package examplepackage
// 正确:大写开头才能被导出
func Examplefile1function() {
// 函数实现
}
customFile.go:
package custompackage
// 正确:大写开头才能被导出
func Customfilefunction() {
// 函数实现
}
修改后运行以下命令:
go mod tidy
go run mainfile.go
如果问题仍然存在,请检查examplefile2.go文件是否在正确的包目录中,并且确保文件已保存。

