登录
首页 >  Golang >  Go问答

遇到问题:在 Go 中声明堆栈集合

来源:stackoverflow

时间:2024-02-23 10:00:22 438浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《遇到问题:在 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 不使用分号来终止行。


解决方案


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

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

本篇关于《遇到问题:在 Go 中声明堆栈集合》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>