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

我正在使用 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.go


package main


import (

    "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 不使用分号来终止行。



MMTTMM
浏览 62回答 1
1回答

jeck猫

不要初始化结构定义中的值,只需设置类型。创建结构体的新实例时初始化该值。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(),    }}
打开App,查看更多内容
随时随地看视频慕课网APP