drrhr20884 2011-10-12 15:34
浏览 52
已采纳

Go编程语言中的任何类型和实现通用列表

I'm trying a little of go programming language.

I'm a excited about the simplicity of the Go, but after playing with it I met some troubles.

1 . I know that Go doesn't support generics and inheritance. Is there any way to implement generic list?

I thinking about using:

type Any interface { }

but how can I check if the value is NULL.
I'm looking for some equivalent implementation to C

struct List {
  List* tail;
  void* head;
}

Or using algebraic datatype:

data List a = Nil | Cons a (List a)


2 . More advanced requirement would be to make some container for objects with a field of a particular type?
For example in Scala programming language I can type:

val List[Animal { type SuitableFood = Grass} ]

to get a List of Animals, which have a member type SuitableFood which is Grass

  • 写回答

2条回答 默认 最新

  • dongqu1783 2011-10-12 17:14
    关注

    It sound's like what you're trying to make is a linked list.

    Here's an example of one included with golang:
    http://golang.org/pkg/container/list/

    And here's the source code:
    http://golang.org/src/pkg/container/list/list.go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM