我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

如何在golang中实现c ++ template <T>

How to translate this cpp code to golang ?

template<T> class CppTemp {
    T  a;
    T* pa;
    T foo(T &t);
};

template<T> T foo2(const T &t)

Go doesn't support templates or generics. There are three things you can do:

  • use non-empty interfaces where applicable
  • generate code with go generate
  • use interface{}:

    type GoTemp struct {
        a interface{}
    }
    
    func (gt *GoTemp) foo(v interface{}) {
        // ...
    }
    
    func foo2(v interface{}) {
        // ...
    }
    

近期文章

  • 在另一个方法中未解析的方法调用引用
  • golang bcrypt返回的字节数
  • 解码失败时获取原始XML
  • 获取文件绝对路径时出现Golang运行时错误
  • 禁用Golang TCP中的截止日期
  • 如何在Golang的文本文件中捕获fmt.Print输出
  • 这些方法是否在MongoDB服务器中创建新的数据库和集合
  • golang编译时间(静态代码分析)以检测格式字符串和参数之间的不匹配
  • Golang开关有一个`用作值`错误?
  • 生成具有整数范围的数组
  • 在Go中解析RSS feed
  • 忽略CTRL-C时从用户读取输入
  • 可以在Go中使用类似“ freopen”的构造吗?
  • 在开放端口上打开URL
  • 如何使用反射将数组值设置为golang中的字段?
  • 在Go中解析表单数组
  • 为什么在执行相同的按位和I / O操作时,我的Rust程序比Go程序慢4倍? [重复]
  • 用于验证URL中“ /”的正则表达式
  • 在结构内部引用自己
  • Golang Sort为地图添加了额外的值

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华