doujiu3097 2016-07-20 23:04
浏览 399
已采纳

如何在golang中实现void *? [重复]

This question already has an answer here:

I'm making a (non-binary) tree in Golang.

In Java, I'd use something like

 class Node{
   List <Object> data;
 }

Where data can be either a pointer to data or to a child node

In C or C++, I'd use a void*.

What type should I use in golang

</div>
  • 写回答

2条回答 默认 最新

  • dtv995719 2016-07-21 00:17
    关注

    I've never tried to solve this problem in any language, would this work in Go?

    go play link edit after reading the duplicate question i see you're not asking about the implementation, enjoy anyway ♥︎

    package main
    
    import (
        "fmt"
    )
    
    type Node struct {
        List []*Node
        Data interface{}
    }
    
    func main() {
        trivial := Node{}
    
        leaf := Node{Data: 1}
    
        tree := Node{
            List: []*Node{
                &leaf,
                &leaf,
            },
        }
        out(trivial)
        out(leaf)
        out(tree)
    }
    
    func out(x interface{}) {
        fmt.Printf("%T:%+v
    ", x, x)
    }
    
    // main.Node:{List:[] Data:<nil>}
    // main.Node:{List:[] Data:1}
    // main.Node:{List:[0xc82000e180 0xc82000e180] Data:<nil>}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 串口发送数据和接收数据
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作
  • ¥15 做过TCL海信电视小米电视相关影视会员软件私我