在Go中导入”net”包时出现错误。

huangapple go评论81阅读模式
英文:

"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

huangapple
  • 本文由 发表于 2012年4月26日 08:05:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/10325525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定