【问题标题】:C++ Template-like behavior in GoGo 中的 C++ 模板类行为
【发布时间】:2016-09-18 15:23:01
【问题描述】:

例如,我有两个不同的结构 FooBar

struct Foo {...}
struct Bar {...}

以及许多基于它们构建的函数和其他类型。

C++ 风味:

template<typename T>
struct Identified {
    T model;
    std::string id;
};

template<typename T>
Identified<T> GetIdentifiedModel(std::string id) {
    Identified<T> result;
    T.id = id;
    T.model.set(getSomeData(id));  // Common method for T
    return result;
}

如何在 Go 中实现这些示例?

对于常用方法,接口可以完成这项工作,但我不知道如何从接口中检索特定类型来声明它、返回它或其他任何东西,而且我不能再处理复制/粘贴代码了 :)

谢谢!

编辑@Amd:https://ideone.com/rqpsQb

#include <string>
#include <iostream>

struct Foo {
    char c;
    void set(std::string s) {c = s[0];}; // We don't really care here
};
struct Bar {
    int n;
    void set(std::string s) {n = s.size();}; // We don't really care here
};

template<typename T>
struct Identified {
    T model;
    std::string id;
};

template<typename T>
Identified<T> GetIdentifiedModel(std::string id) {
    Identified<T> result;
    result.id = id;
    // Obviously shouldn't be ID but for the example
    result.model.set(id);  // Common method for T
    return result;
}

void assert(bool b) {
    if (b) std::cout << "OK" << std::endl;
    else std::cout << "There is a problem !" << std::endl;
};

int main() {
    auto fooWithID = GetIdentifiedModel<Foo>("foo id");
    auto barWithID = GetIdentifiedModel<Bar>("bar");
    assert (fooWithID.model.c == 'f');
    assert (barWithID.model.n == 3);
    return (0);
}

【问题讨论】:

  • 缺乏泛型/模板是对 golang 的主要批评。你不能这样做,至少在谷歌将它们添加之前
  • @DavidHaim:缺乏泛型/模板是 golang 的一大特点。 :-)
  • @KerrekSB 如果重新实现每个机制 10 次是一项功能,我同意

标签: c++ templates go


【解决方案1】:

1- 你可以使用

fooWithID := GetIdentifiedModel("foo id", &Foo{})

喜欢这个工作示例(试试The Go Playground):

package main

import "fmt"

type Foo struct {
    c byte
}

func (t *Foo) set(s string) { t.c = s[0] }

type Bar struct {
    n int
}

func (t *Bar) set(s string) { t.n = len(s) }

type Identified struct {
    model T
    id    string
}

func GetIdentifiedModel(id string, t T) *Identified {
    result := &Identified{model: t}
    result.id = id
    result.model.set(id)
    return result
}

func assert(b bool) {
    if b {
        fmt.Println("OK")
    } else {
        fmt.Println("There is a problem !")
    }
}

func main() {
    fooWithID := GetIdentifiedModel("foo id", &Foo{})
    barWithID := GetIdentifiedModel("bar", &Bar{})

    assert(fooWithID.model.(*Foo).c == 'f')
    assert(barWithID.model.(*Bar).n == 3)
}

type T interface {
    set(string)
}

输出:

OK
OK

2- 你可以使用(这很好读:Identified model Foo):

fooWithID := GetIdentifiedModel("foo id", &Identified{model: &Foo{}})

喜欢这个工作示例(试试The Go Playground):

package main

import "fmt"

type Foo struct {
    c byte
}

func (t *Foo) set(s string) { t.c = s[0] }

type Bar struct {
    n int
}

func (t *Bar) set(s string) { t.n = len(s) }

type Identified struct {
    model T
    id    string
}

func GetIdentifiedModel(id string, result *Identified) *Identified {
    result.id = id
    result.model.set(id)
    return result
}

func assert(b bool) {
    if b {
        fmt.Println("OK")
    } else {
        fmt.Println("There is a problem !")
    }
}

func main() {
    fooWithID := GetIdentifiedModel("foo id", &Identified{model: &Foo{}})
    barWithID := GetIdentifiedModel("bar", &Identified{model: &Bar{}})    
    assert(fooWithID.model.(*Foo).c == 'f')
    assert(barWithID.model.(*Bar).n == 3)
}

type T interface {
    set(string)
}

输出:

OK
OK

另见:One method to handle all the struct types that embed one common struct (json marshalling)

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    相关资源
    最近更新 更多