How do I set GODEBUG environment variables in Golang so I can use godebug with net/http

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

How do I set GODEBUG environment variables in Golang so I can use godebug with net/http

问题

我想使用Godebug来逐步执行我的程序。然而,由于我正在使用net/http,所以我会遇到错误,例如:

/home/heath/go/src/net/http/h2_bundle.go:45:2: could not import golang_org/x/net/http2/hpack (cannot find package "golang_org/x/net/http2/hpack" in any of:
    /home/heath/go/src/golang_org/x/net/http2/hpack (from $GOROOT)

/x/net/http2/hpack在我的GOPATH中确实存在,但在~heath/go/src/golang.org中而不是golang_org(不确定发生了什么)。

我已经阅读到这个错误是因为godebug还不支持http2(找不到源代码)。

我尝试通过在init()函数和命令行中设置GODEBUG环境变量来禁用http2server和http2client。我还通过执行fmt.Println("GODEBUG", os.Getenv("GODEBUG"))来确认这些设置已经生效,具体说明请参考这里

GODEBUG=http2client=0  # 禁用HTTP/2客户端支持
GODEBUG=http2server=0  # 禁用HTTP/2服务器支持

我提供了一个简单的代码示例来复现这个错误:

package main

import "fmt"
import "net/http"
import "os"

func init() {

    os.Setenv("GODEBUG", "http2server=0,http2client=0")

}

func main() {

    fmt.Println("GODEBUG", os.Getenv("GODEBUG"))

    _ = "breakpoint"
    fmt.Println("Hello, World!")
    http.ListenAndServe(":8080", nil)
}

使用以下命令运行代码:

godebug run example.go

我正在运行的Go版本是:

go version go1.7 linux/amd64

英文:

I want to step through my program using Godebug. However because I'm using net/http I get errors such as:

/home/heath/go/src/net/http/h2_bundle.go:45:2: could not import golang_org/x/net/http2/hpack (cannot find package "golang_org/x/net/http2/hpack" in any of:
    /home/heath/go/src/golang_org/x/net/http2/hpack (from $GOROOT)

/x/net/http2/hpack does exist in my GOPATH but in ~heath/go/src/golang.org ... not golang_org (not sure what's happening there)

I have read that this error occurs because godebug doesn't support http2 yet (can't find source).

I have tried to disable http2server and http2client by setting the GODEBUG env both in init() and on the command line. I have also confirmed that these settings are being set by doing a fmt.Println("GODEBUG", os.Getenv("GODEBUG"). As per instructions located here

GODEBUG=http2client=0  # disable HTTP/2 client support
GODEBUG=http2server=0  # disable HTTP/2 server support

My simple code example to replicate the error is:

  package main
  
  import "fmt"
  import "net/http"
  import "os"
  
  func init() {
  
      os.Setenv("GODEBUG", "http2server=0,http2client=0")
 
  }
 
  func main() {
 
      fmt.Println("GODEBUG", os.Getenv("GODEBUG"))
 
      _ = "breakpoint"
      fmt.Println("Hello, World!")
      http.ListenAndServe(":8080", nil)
  }

godebug run example.go

I am running Go version:

go version go1.7 linux/amd64

答案1

得分: 0

这可能不是正确的做法。但是我将http2包从$GOROOT/src/vendor目录下的供应商目录复制到$GOPATH/src目录下的src目录中。

如果有人对正确引用供应商目录有任何进一步的建议,请分享你的想法。我在这里提供我的解决方法,以防其他人遇到相同的问题。

编辑:实际上,一个更好的做法是使用ln -s ..src/vender/github_org到../src/github_org来完成。

英文:

This is probably not the correct way to do things. But I copied the http2 package from my vendor directory under $GOROOT/src/vendor to the src directory under my $GOPATH/src.

If anyone has any further input on the correct way to reference vendor directories please add your thoughts. Just putting my workaround here in case someone else comes across the same issue.

Edit: Actually a 'nicer' way to do things was to do an ln -s ..src/vender/github_org to ../src/github_org

huangapple
  • 本文由 发表于 2016年8月24日 08:20:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/39112605.html
匿名

发表评论

匿名网友

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

确定