1

我正在尝试在本地运行generateCGO 项目https://github.com/libgit2/git2go 。我已经成功安装了系统库,并且可以使用和libgit2构建和测试项目。go build -tags static,system_libgit2go test -tags static,system_libgit2

当我尝试使用stringer. 我显示的代码位于 master 分支中,所以我想它应该可以正常工作,问题就在我这边。

文件diff.go有 stringer 注释(删除了不重要的部分):

package git
/*
#include <git2.h>

...
*/
import "C"
import (
    "errors"
    "runtime"
    "unsafe"
)

...

type Delta int

const (
    DeltaUnmodified Delta = C.GIT_DELTA_UNMODIFIED
    DeltaAdded      Delta = C.GIT_DELTA_ADDED
    DeltaDeleted    Delta = C.GIT_DELTA_DELETED
    DeltaModified   Delta = C.GIT_DELTA_MODIFIED
    DeltaRenamed    Delta = C.GIT_DELTA_RENAMED
    DeltaCopied     Delta = C.GIT_DELTA_COPIED
    DeltaIgnored    Delta = C.GIT_DELTA_IGNORED
    DeltaUntracked  Delta = C.GIT_DELTA_UNTRACKED
    DeltaTypeChange Delta = C.GIT_DELTA_TYPECHANGE
    DeltaUnreadable Delta = C.GIT_DELTA_UNREADABLE
    DeltaConflicted Delta = C.GIT_DELTA_CONFLICTED
)

//go:generate stringer -type Delta -trimprefix Delta -tags static

...

此类型引用libgit2C 枚举,即/usr/include/git2/diff.h

/**
 * What type of change is described by a git_diff_delta?
 *
 * `GIT_DELTA_RENAMED` and `GIT_DELTA_COPIED` will only show up if you run
 * `git_diff_find_similar()` on the diff object.
 *
 * `GIT_DELTA_TYPECHANGE` only shows up given `GIT_DIFF_INCLUDE_TYPECHANGE`
 * in the option flags (otherwise type changes will be split into ADDED /
 * DELETED pairs).
 */
typedef enum {
    GIT_DELTA_UNMODIFIED = 0,  /**< no changes */
    GIT_DELTA_ADDED = 1,      /**< entry does not exist in old version */
    GIT_DELTA_DELETED = 2,    /**< entry does not exist in new version */
    GIT_DELTA_MODIFIED = 3,    /**< entry content changed between old and new */
    GIT_DELTA_RENAMED = 4,     /**< entry was renamed between old and new */
    GIT_DELTA_COPIED = 5,      /**< entry was copied from another old entry */
    GIT_DELTA_IGNORED = 6,     /**< entry is ignored item in workdir */
    GIT_DELTA_UNTRACKED = 7,   /**< entry is untracked item in workdir */
    GIT_DELTA_TYPECHANGE = 8,  /**< type of entry changed between old and new */
    GIT_DELTA_UNREADABLE = 9,  /**< entry is unreadable */
    GIT_DELTA_CONFLICTED = 10, /**< entry in the index is conflicted */
} git_delta_t;

当我运行命令时go generate出现错误:

stringer: can't happen: constant is not an integer DeltaUnmodified
diff.go:43: running "stringer": exit status 1

我试图运行这些命令:

  • go generate ./diff.go
  • go generate -tags static,system_libgit2
  • go generate -tags static,system_libgit2 ./diff.go

但它总是显示相同的错误。

如何正确生成带有来自 C 枚举值的 Go 常量的 stringer 文件?

4

1 回答 1

1

stringer通过简单地解析Go 源代码go.ast来提取所需的常量值。这些值必须是在定义站点指定的整数文字(参见源代码)。

CGo 通过为 C 代码生成 Go 垫片来工作。例如,C 常量作为shims进入_cgo_gotypes.go文件。const _Ciconst_... = ...

通常 CGo 会在完成后删除生成的文件,但您可以通过显式调用它来保留它们,例如go tool cgo main.go.

所以你应该能够做这样的事情:

go tool cgo main.go
mv _obj/_cgo_gotypes.go _obj/cgo_gotypes.go
stringer -type Delta -trimprefix Delta ./_obj
cp _obj/delta_strings.go .
于 2021-10-14T11:15:28.260 回答