Golang:如何通过struct name(string)实例化`struct`

时间:2014-05-06 05:45:11

标签: struct go

不在golang, 我们可以通过类名动态地实例化类

PHP

<?php
class MyKlass {
    public $prop = "foo";
    public function meth() {
        echo "This is my method\n";
        echo "This.prop = {$this->prop}";
    }
}
$class_name = "MyKlass";
$k = new $class_name;
$k->meth();

的Python

class MyKlass:
    def __init__(self):
        self.prop = "buz"
    def meth(self):
        print("This prop is " + self.prop)
modules = {
    "MyKlass":MyKlass
}
class_name = "MyKlass"
k = modules[class_name]()
k.meth()

所以在golang, 如何使用结构名称实例化struct,这是string类型?

0 个答案:

没有答案