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

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 h3.6m 人类行为预测论文复现
  • ¥50 wordpress项目注册报失败刷新后其实是成功状态,请求排查原因
  • ¥20 linxu服务器僵尸进程不释放,代码如何修改?
  • ¥15 pycharm激活不成功
  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)