目录
GO
练习的项目结构
@:~/goProject/test.cn$ tree
.
├── go.mod
├── main.go
└── model
└── mysql.go
1 directory, 3 files
Q1 导入的是空路径
build command-line-arguments: cannot load /home/vagrant/aaron/goProject/test.cn/model: malformed module path "/home/vagrant/aaron/goProject/test.cn/model": empty path element
main.go
package main
import (
"/home/vagrant/aaron/goProject/test.cn/model"
)
func main(){
model.Print()
}
Q2 导入的路径不全
vagrant@aaron:~/aaron/goProject/test.cn$ go run main.go
build command-line-arguments: cannot load model: malformed module path "model": missing dot in first path element
main.go
package main
import (
"model" // 这个路径不全
)
func main(){
model.Print()
}
Q3 找不到路径
vagrant@aaron:~/aaron/goProject/test.cn$ go run main.go
build command-line-arguments: cannot load test.cn/model: cannot find module providing package test.cn/model
A3
这个失败是在test.cn目录下缺少go.mod
文件
Q4 函数不可调用
vagrant@aaron:~/aaron/goProject/test.cn$ go run main.go
# command-line-arguments
./main.go:9:2: cannot refer to unexported name model.Print
./main.go:9:2: undefined: model.Print
main.go
package main
import (
"test.cn/model"
)
func main(){
model.Print()
}
A4
这个失败的原因是调用的函数是不是公开的函数,如需让外部调用函数,需要函数名的首字母为大写。
Q5 报错Use as value
vagrant@aaron:~/aaron/goProject/test.cn$ go run main.go
# command-line-arguments
./main.go:9:2: undefined: fmt.Pirintln
./main.go:9:27: model.Print() used as value
mysql.go
func Print() {
fmt.Println("I am Print")
return
}
A5
报错原因是调用的函数没有返回值