英文:
"net" package import error in Go
问题
我正在尝试运行在网上找到的一个示例Go程序,如下所示:
/* IP */
package main
import (
"net"
"os"
"fmt"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s ip-addr\n", os.Args[0])
os.Exit(1)
}
name := os.Args[1]
addr := net.ParseIP(name)
if addr == nil {
fmt.Println("Invalid address")
} else {
fmt.Println("The address is ", addr.String())
}
os.Exit(0)
}
然后我尝试使用以下命令进行编译:
6g ip.go
然后我得到以下错误:
ip.go:7: can't find import: net
我的Go版本是否没有net包?还是我使用了错误的编译器版本?谢谢!
英文:
I'm trying to run a sample Go program that I found on the net, which is given below:
/* IP */
package main
import (
"net"
"os"
"fmt"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s ip-addr\n", os.Args[0])
os.Exit(1)
}
name := os.Args[1]
addr := net.ParseIP(name)
if addr == nil {
fmt.Println("Invalid address")
} else {
fmt.Println("The address is ", addr.String())
}
os.Exit(0)
}
Then I try to compile it using:
6g ip.go
and I get the following error:
ip.go:7: can't find import: net
Does my go version not have a net package? or am I using a wrong version of compiler? Thanks!
答案1
得分: 3
如果您仍在使用6g
编译器命令,那么我假设您没有使用最新的Go1稳定版本?这将取决于您实际运行的版本。"net"是一个有效的包:http://golang.org/pkg/net/
建议您安装最新的Go。
$ go run ip.go 127.0.0.1
地址是 127.0.0.1
您可以看到,使用Go 1的Go Playground可以正常工作:
http://play.golang.org/p/rXSep9GH-U
英文:
If you are still using the 6g
compiler command then I assume you are not using the recent Go1 stable release? It would depend on the version you are actually running. "net" is a valid package: http://golang.org/pkg/net/
Recommended that you install the latest Go.
$ go run ip.go 127.0.0.1
The address is 127.0.0.1
You can see that the go playground, using Go 1, works:
http://play.golang.org/p/rXSep9GH-U
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论