为 golang 依赖模块使用特定版本

Using a particular version for golang dependency module

我正在尝试从 github link 构建 postfix-exporter 代码。 它依赖于 go-systemd 包,如 go.mod 文件 github.com/coreos/go-systemd/v22 v22.0.0 中所述。 我在 go.mod 文件中看到,提到的包版本是 v22.0.0. 但是当我为这个路径 运行 go get -u 时,它开始下载 go-systemd 的最新版本(v22.2.0),这在最新的提交中有问题并导致编译失败。 出现的错误是

github.com/coreos/go-systemd/v22@v22.2.0/sdjournal/journal.go:313:60: error: '_SD_ARRAY_STATIC' undeclared here (not in a function) // my_sd_id128_to_string(void *f, sd_id128_t boot_id, char s[_SD_ARRAY_STATIC SD_ID128_STRING_MAX]) ^ In file included from /usr/include/systemd/sd-journal.h:31:0, from pkg/mod/github.com/coreos/go-systemd/v22@v22.2.0/sdjournal/journal.go:27: pkg/mod/github.com/coreos/go-systemd/v22@v22.2.0/sdjournal/journal.go:313:77: error: expected ']' before numeric constant // my_sd_id128_to_string(void *f, sd_id128_t boot_id, char s[_SD_ARRAY_STATIC SD_ID128_STRING_MAX])

我想知道如何编译任何依赖模块的特定版本,如果这不是这种方式,或者我缺少一些需要的选项来支持 go.mod[ 中提到的依赖包的版本=21=]

非常感谢,请原谅我的 golang 知识。

不要使用 -u-u 的目的是让 Go 尝试将您升级到最新的次要版本或补丁版本:

The -u flag instructs get to update modules providing dependencies of packages named on the command line to use newer minor or patch releases when available.

如果您只是想安装依赖项,请使用 go get

我试图为 Centos7 构建 podman 并发现了这个错误,注意到 _SD_ARRAY_STATIC 没有定义,所以我只是在 google 中搜索并找到了这个头文件:https://code.woboq.org/qt5/include/systemd/_sd-common.h.html. 另外,通过在我的 docker 中搜索这个文件,我发现了这个非常旧的文件:/usr/include/systemd/_sd-common.h,所以我决定修改它并添加该定义:

/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

#ifndef foosdcommonhfoo
#define foosdcommonhfoo

/***
  This file is part of systemd.

  Copyright 2013 Lennart Poettering
...
...
#ifndef _SD_END_DECLARATIONS
#  ifdef __cplusplus
#    define _SD_END_DECLARATIONS                                \
        }                                                       \
        struct __useless_struct_to_allow_trailing_semicolon__
#  else
#    define _SD_END_DECLARATIONS                                \
        struct __useless_struct_to_allow_trailing_semicolon__
#  endif
#endif

#ifndef _SD_ARRAY_STATIC
#  if __STDC_VERSION__ >= 199901L
#    define _SD_ARRAY_STATIC static
#  else
#    define _SD_ARRAY_STATIC
#  endif
#endif

#endif

瞧,它开始工作了。 TL;DR,可能您必须更新 systemd 包或至少更新 systemd C 库。