doubi1818 2015-06-06 19:18
浏览 225
已采纳

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

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条回答 默认 最新

  • dongniechi7825 2015-06-06 19:33
    关注

    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() {}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 如何判断设置快捷键来实现隐藏/显示窗口
  • ¥15 关于#材料工程#的问题:有没有具有电子阻挡层和空穴阻挡层的电池仿真silvaco代码例子或者其他器件具有阻挡层例子的silvaco代码(最好还有相关文献)
  • ¥60 基于MATLAB的TAOD算法
  • ¥15 Groimp使用疑问
  • ¥15 MDK–ARM里一直找不到调试器
  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图
  • ¥15 Dev-c++打字不出现中文,但出现日文