go程序抛出错误,找不到包”code.google.com/p/go.net/websocket”。

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

go program throwing error cannot find package "code.google.com/p/go.net/websocket"

问题

我是你的中文翻译助手,以下是你的代码的翻译:

我是Go编程语言的新手以下是我的代码

package main

import (
    "code.google.com/p/go.net/websocket"
    "fmt"
    "net/http"
    "strconv"
)

var add := "12345"

func EchoLengthServer(ws *webscoket.Conn) {
    var msg string

    for {
        websocket.Message.Receive(ws, &msg)
        fmt.Println("收到消息", msg)
        length := len(msg)
        if err := websocket.Message.Send(ws, strconv.FormatInt(int64(length), 10)); err != nil {
            fmt.Println("无法发送消息长度")
            break
        }
    }
}

func websocketListen() {
    http.Handle("/length", websocket.Handler(EchoLengthServer))
    err := http.ListenAndServe(addr, nil)
    if err != nil {
        panic("ListenAndServe:" + err.Error())
    }
}

当我执行这段代码时,我得到以下错误:

[rajkumar@localhost ch4-DesigningAPI]$ go run WebSockets.go 
WebSockets.go:6:3: 找不到包 "code.google.com/p/go.net/websocket",在以下位置中都找不到:
    /usr/local/go/src/code.google.com/p/go.net/websocket (来自 $GOROOT)
    /home/rajkumar/GOPackages/src/code.google.com/p/go.net/websocket (来自 $GOPATH)

我尝试使用go get命令将websocket包添加到GOPATH中,但是也出现以下错误:

[rajkumar@localhost ch4-DesigningAPI]$ go get code.google.com/p/go.net/websocket
go: 缺少Mercurial命令。请参阅 http://golang.org/s/gogetcmd
package code.google.com/p/go.net/websocket: exec: "hg": 在$PATH中找不到可执行文件

请问你能帮我解决这个错误吗?

英文:

I am new to Go programming language. Below is my code.

package main

import (
	"code.google.com/p/go.net/websocket"
	"fmt"
	"net/http"
	"strconv"
)

var add := "12345"

func EchoLengthServer(ws *webscoket.Conn) {
	var msg string

	for {
		websocket.Message.Receive(ws, &msg)
		fmt.Println("Got Message", msg)
		length := len(msg)
		if err := websocket.Message.Send(ws, strconv.FormatInt(int64(length), 10)); err != nil {
			fmt.Println("can't send message length")
			break
		}
	}
}

func websocketListen() {
	http.Handle("/length", websocket.Handler(EchoLengthServer))
	err := http.ListenAndServe(addr, nil)
	if err != nil {
		panic("ListenAndServe:" + err.Error())
	}
}

when I executed the code i got below error

[rajkumar@localhost ch4-DesigningAPI]$ go run WebSockets.go 
WebSockets.go:6:3: cannot find package "code.google.com/p/go.net/websocket" in any of:
	/usr/local/go/src/code.google.com/p/go.net/websocket (from $GOROOT)
	/home/rajkumar/GOPackages/src/code.google.com/p/go.net/websocket (from $GOPATH)

I tried to add websocket package in GOPATH by go get command but that is also throwing below error

[rajkumar@localhost ch4-DesigningAPI]$ go get code.google.com/p/go.net/websocket
go: missing Mercurial command. See http://golang.org/s/gogetcmd
package code.google.com/p/go.net/websocket: exec: "hg": executable file not found in $PATH

Could you please help me in resolving this error.

答案1

得分: 15

这样怎么样?

$ go get -v golang.org/x/net/websocket
golang.org/x/net/websocket
$
package main

import (
    "fmt"
    "net/http"
    "strconv"

    "golang.org/x/net/websocket"
)

var addr = "12345"

func EchoLengthServer(ws *websocket.Conn) {
    var msg string

    for {
        websocket.Message.Receive(ws, &msg)
        fmt.Println("收到消息", msg)
        length := len(msg)
        if err := websocket.Message.Send(ws, strconv.FormatInt(int64(length), 10)); err != nil {
            fmt.Println("无法发送消息长度")
            break
        }
    }
}

func websocketListen() {
    http.Handle("/length", websocket.Handler(EchoLengthServer))
    err := http.ListenAndServe(addr, nil)
    if err != nil {
        panic("ListenAndServe:" + err.Error())
    }
}

func main() {}
英文:

How about this?

$ go get -v golang.org/x/net/websocket
golang.org/x/net/websocket
$

-

package main

import (
	"fmt"
	"net/http"
	"strconv"

	"golang.org/x/net/websocket"
)

var addr = "12345"

func EchoLengthServer(ws *websocket.Conn) {
	var msg string

	for {
		websocket.Message.Receive(ws, &msg)
		fmt.Println("Got Message", msg)
		length := len(msg)
		if err := websocket.Message.Send(ws, strconv.FormatInt(int64(length), 10)); err != nil {
			fmt.Println("can't send message length")
			break
		}
	}
}

func websocketListen() {
	http.Handle("/length", websocket.Handler(EchoLengthServer))
	err := http.ListenAndServe(addr, nil)
	if err != nil {
		panic("ListenAndServe:" + err.Error())
	}
}

func main() {}

huangapple
  • 本文由 发表于 2015年6月7日 03:18:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/30686655.html
匿名

发表评论

匿名网友

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

确定