go program throw错误无法找到包" code.google.com/p/go.net/websocket"

时间:2015-06-06 19:18:51

标签: go

我是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("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())
    }
}

当我执行代码时,我得到了以下错误

[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)

我尝试通过go get命令在GOPATH中添加websocket包,但这也是在错误下面抛出

[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

请你帮我解决这个错误。

1 个答案:

答案 0 :(得分:13)

这个怎么样?

$ 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() {}