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 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂