为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Go 的堆栈集合声明堆栈时遇到问题

使用 Go 的堆栈集合声明堆栈时遇到问题

Go
MMTTMM 2023-07-10 15:04:19
我正在使用 VS Code 将最初用 C++ 编写的工具转换为 Go,但 Go linter 不喜欢我的堆栈声明。我已经根据 Go 文档导入了堆栈集合,并且我认为我的 go 工作区目录层次结构是正确的。-go (workspace)    -bin    -pkg        -darwin_amd64            -github.com                -golang-collections                    -collections                        -stack.a    -src        -github.com            -golang-collections                -collections                    -stack                        stack.go                        stack_test.go            -zwnewsom                -verifier                   main.gopackage mainimport (    "C"    "github.com/golang-collections/collections/stack")type Item struct {    key   int    value int    //sum   int    sum float64    numerator   int64    denominator int64    exponent float64    status Status    promoteItems := stack.New()}'New()' 函数应该返回一个指向堆栈的指针,但 VS Code Go linter 在 ':= stack.New()' 下显示黄色波浪线,并显示错误“预期 ';',发现 ':=' “这是双重令人困惑的,因为我的印象是 Go 不使用分号来终止行。
查看完整描述

1 回答

?
jeck猫

TA贡献1909条经验 获得超7个赞

不要初始化结构定义中的值,只需设置类型。创建结构体的新实例时初始化该值。


type Item struct {

    key   int

    value int

    //sum   int

    sum float64


    numerator   int64

    denominator int64


    exponent float64


    status Status


    promoteItems stack.Stack

}


func main() {

    // create an instance of struct Item

    item := Item{

        promoteItems: stack.New(),

    }

}


查看完整回答
反对 回复 2023-07-10
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信