dongye9820 2013-07-06 17:39
浏览 64
已采纳

来自自定义结构的Golang参考列表

I have the following block of code:

package main

import (
    "fmt"
    "container/list"
)

type Foo struct {
    foo list  //want a reference to the list implementation   
             //supplied by the language
}


func main() {
   //empty  

}

When compiling I receive the following message:

use of package list not in selector

My question is, how do I reference list within a struct? Or is this not the proper idiom in Go for wrapping structures. (Composition)

  • 写回答

1条回答 默认 最新

  • doushi7761 2013-07-06 18:01
    关注

    I can see two problems:

    1. importing the fmt package without using it. In Go unused imports result in compile-time errors;
    2. foo is not declared correctly: list is a package name not a type; you want to use a type from the container/list package.

    Corrected code:

    package main
    
    import (
        "container/list"
    )
    
    type Foo struct {
        // list.List represents a doubly linked list.
        // The zero value for list.List is an empty list ready to use.
        foo list.List
    }
    
    func main() {}
    

    You can execute the above code in the Go Playground.
    You should also consider reading the official documentation of the container/list package.

    Depending on what you're trying to do, you might also want to know that Go allows you to embed types within a struct or interface. Read more in the Effective Go guide and decide wether or not this makes sense for your particular case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了