douyi3676 2016-02-27 12:22
浏览 239
已采纳

给定可执行文件,我可以确定用于构建它的GOOS和GOARCH的值吗?

The title pretty much sums it up. How can I determine what the values of GOOS and GOARCH was given just the go executable?

  • 写回答

1条回答 默认 最新

  • duanrong3308 2016-02-27 12:32
    关注

    EDIT: The behavior of runtime.GOROOT() changed in Go 1.10, for details, see Go 1.10 release notes # Runtime. Basically now runtime.GOROOT() checks if the GOROOT environment variable is set, and if so, its value is returned. If not, it returns the GOROOT value recorded at compile time.


    Check out the runtime package:

    The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete the set of Go environment variables. They influence the building of Go programs (see https://golang.org/cmd/go and https://golang.org/pkg/go/build). GOARCH, GOOS, and GOROOT are recorded at compile time and made available by constants or functions in this package, but they do not influence the execution of the run-time system.

    A list of possible combinations for GOARCH and GOOS can be found here: https://golang.org/doc/install/source#environment

    So what you are looking for are constants in the runtime package:

    runtime.GOOS
    runtime.GOARCH
    

    And they will exactly contain the values that were present when your app was built.

    For example see this simple app:

    func main() {
        fmt.Println(runtime.GOOS)
        fmt.Println(runtime.GOARCH)
    }
    

    Let's say GOOS=windows and GOARCH=amd64. Running it with go run xx.go will print:

    windows
    amd64
    

    Build an exe from it (e.g. go build). Running the exe has the same output.

    Now change GOARCH to 386. If you run it with go run (or build an exe and run that), it will print:

    windows
    386
    

    If you run the previously built exe, it will still print:

    windows
    amd64
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题