doujiao2000 2019-08-06 02:51
浏览 10
已采纳

使用Go的堆栈集合声明堆栈时遇到麻烦

I'm translating a tool originally written in C++ to Go using VS Code, but the Go linter doesn't like my stack declaration.

I've imported the stack collection per the Go documentation and I think my go workspace directory hierarchy is correct.

-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()
}

the 'New()' function should return a pointer to a stack but the VS Code Go linter gives me a yellow squiggly under ':= stack.New()' with an error saying "expected ';', found ':='" which is doubly confusing since I was under the impression that Go doesn't use semicolons to terminate lines.

  • 写回答

1条回答 默认 最新

  • duanchan5458 2019-08-06 03:02
    关注

    Don't initialize values in the struct definition, just set the type. Initialize the value when you create a new instance of the struct.

    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(),
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!