dongqiu9018 2018-02-06 03:43
浏览 10
已采纳

前往:如何处理库API更改?

I originally wrote my project with Go v1.9, which includes the os.LookupEnv function.

Now I need to build with Go v1.4, but that function is missing -- it only has os.Getenv which has slightly different behavior.

In Python, I could provide a compatibility function, something like this:

if 'LookupEnv' not in os:
    def myLookupEnv(k):
        v = os.Getenv(k)
        return v, v!=''
    os.LookupEnv = myLookupEnv

How can I handle these kinds of API changes in Go?

  • 写回答

1条回答 默认 最新

  • drfcaw7460 2018-02-06 06:21
    关注

    Use build constraints. A build constraint is a line comment that begins

    // +build
    

    that lists the conditions under which a file should be included in the package. See Build Constraints in Package build for details.

    For example, for Go 1.5 and later (// +build go1.5), forward LookupEnv to os.LookupEnv. For Go 1.4 and earlier (// +build !go1.5), implement LookupEnv.

    src/lookup/lookup.go:

    package main
    
    import (
        "fmt"
        "runtime"
    )
    
    func main() {
        fmt.Println("version:", runtime.Version())
        key := "HOME"
        value, found := LookupEnv(key)
        fmt.Printf("key: %q value %q found: %t
    ", key, value, found)
    }
    

    src/lookup/env.go:

    // +build go1.5
    
    // Forward LookupEnv to os.LookupEnv
    
    package main
    
    import "os"
    
    // LookupEnv retrieves the value of the environment variable named
    // by the key. If the variable is present in the environment the
    // value (which may be empty) is returned and the boolean is true.
    // Otherwise the returned value will be empty and the boolean will
    // be false.
    func LookupEnv(key string) (string, bool) {
        return os.LookupEnv(key)
    }
    

    src/lookup/env_1.4.go:

    // +build !go1.5
    
    // Implement LookupEnv
    
    package main
    
    import "syscall"
    
    // LookupEnv retrieves the value of the environment variable named
    // by the key. If the variable is present in the environment the
    // value (which may be empty) is returned and the boolean is true.
    // Otherwise the returned value will be empty and the boolean will
    // be false.
    func LookupEnv(key string) (string, bool) {
        return syscall.Getenv(key)
    }
    

    Output:

    $ go build && ./lookup
    version: devel +fd7331a821 Tue Feb 6 05:00:01 2018 +0000
    key: "HOME" value "/home/peter" found: true
    $
    
    $ go1.4 build && ./lookup
    version: go1.4-bootstrap-20170531
    key: "HOME" value "/home/peter" found: true
    $
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?