
src/├── hello_world│ ├── hello.go│ └── math│ └── add.go
hello.go文件包含以下代码:
package mainimport ( "fmt" math "hello_world/math")func main() { fmt.Println("Hello World") x := math.add(6,5)} 并添加.go
package mathfunc add(x,y int) int { return x + y} 当我跑去打招呼时我会看到:
evgen@laptop:~/go/src/hello_world$go run hello.go # command-line-arguments./hello.go:10: cannot refer to unexported name math.add./hello.go:10: undefined: "hello_world/math".add
GOPATH:
evgen@laptop:~/go/src/hello_world$echo $GOPATH/home/evgen/go
如何解决?谢谢!
解决方法 在包之外,只能访问和引用导出的标识符,即以大写字母开头的标识符.所以最简单的解决方法是通过在math.go中将其名称更改为Add()来导出math.add()函数:
func Add(x,y int) int { return x + y} 当然,当你从main.go中提到它时:
x := math.Add(6,5)
作为旁注,请注意,在导入hello_world / math包时,您不必指定新名称来引用其导出的标识符:默认情况下,它将是其导入路径的最后一部分,因此这相当于你的进口:
import ( "fmt" "hello_world/math")总结
以上是内存溢出为你收集整理的Golang导入包全部内容,希望文章能够帮你解决Golang导入包所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)