dqgg25493 2017-06-27 21:47
浏览 127
已采纳

“ golang.org/x/net/ipv4”在Mac上有效,但在Linux上无效

I'm using "golang.org/x/net/ipv4" in order to use its SetTTL function. Unfortunately it does not seem to work on Linux, only on Mac, even though the documentation indicates Linux supports all the functions.

Here's a minimal example of the problem, with a Dockerfile:

main.go:

package main

import (
  "fmt"
  "net"
  "bufio"
  xnet "golang.org/x/net/ipv4"
)

const Host = "google.com"

func main() {
  var err error
  conn, err := net.Dial("tcp4", Host + ":80")
  if err != nil {
    panic(err)
  }
  defer conn.Close()
  xconn := xnet.NewConn(conn)
  err = xconn.SetTTL(5)
  if err != nil {
    panic(err)
  }
  defer xconn.Close()
  fmt.Fprint(conn, "GET / HTTP/1.1
HOST: google.com

")
  firstLine, err := bufio.NewReader(xconn).ReadString('
')
  if err != nil {
    panic(err)
  }
  fmt.Println(firstLine)
}

Dockerfile:

FROM golang:1.8.1-alpine

RUN apk --no-cache add git
RUN go get golang.org/x/net/ipv4
COPY . /go/src/me.com/me/xnetproblem
RUN go install me.com/me/xnetproblem
CMD ["/go/bin/xnetproblem"]

I run this command:

docker build -t xnet .

I get this output:

john xnetproblem > docker build -t xnet .
Sending build context to Docker daemon 90.62 kB
Step 1/6 : FROM golang:1.8.1-alpine
[snip]
Step 5/6 : RUN go install me.com/me/xnetproblem
 ---> Running in c3802fe61d63
# me.com/me/xnetproblem
src/me.com/me/xnetproblem/main.go:25: xconn.Close undefined (type *ipv4.Conn has no field or method Close)
src/me.com/me/xnetproblem/main.go:28: cannot use xconn (type *ipv4.Conn) as type io.Reader in argument to bufio.NewReader:
    *ipv4.Conn does not implement io.Reader (missing Read method)
The command '/bin/sh -c go install me.com/me/xnetproblem' returned a non-zero code: 2

Using go install natively, instead of Docker, the program works on Mac but not on Linux.

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 和您碰到了同一个问题
      • ¥15 如何在微信支付V3PHP版请求中添加header?
      • ¥15 在数字信号处理中用MATLAB显示波形。
      • ¥15 keil MDK软件进行仿真调试的局限
      • ¥15 关于gateway产生临时文件夹的问题
      • ¥15 Java Broken Pipe以及OOM
      • ¥20 请问有没有RTL8211FS的参考原理图
      • ¥50 esp32的platfromio工程添加文件就会编译失败
      • ¥15 linux下运行VASP报错
      • ¥15 python里面查找赋值,如何可以在剩余未空值的行里面进行进一步的筛选呢?