phpIsNumberOne
V2EX  ›  问与答

[Golang]的建立约束怎么解决?

  •  
  •   phpIsNumberOne · Mar 2, 2021 · 1234 views
    This topic created in 2079 days ago, the information mentioned may be changed or developed.

    我用一个需求,使用 go 实现了;但是是分系统的,准确的说是分别写了个 Windows 下的函数和非 Windows 下的函数。在 Windows 系统下的函数引用了包golang.org/x/sys/windows。

    然后调用的时候类似这样:

    if runtime.GOOS == "windows"{
        //....
    }else{
       //...
    }
    
    

    运行的时候就报了构建约束的错误,难道就不能把他两放一起了?

    2 replies  •  2021-03-02 21:27:52 +08:00
    Evrins
    Evrins
    Mar 2, 2021❤️1
    1
    以这个包为例
    github.com/zach-klippenstein/goadb

    executable.go
    executable_unix.go
    executable_win.go

    // +build windows
    // +build darwin freebsd linux netbsd openbsd

    区分不同系统编译
    phpIsNumberOne
    phpIsNumberOne
    OP
    Mar 2, 2021
    2
    @Evrins 非常感谢,我已经彻底悟了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1485 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 16:38 · PVG 00:38 · LAX 08:38 · JFK 11:38
    ♥ Do have faith in what you're doing.