不在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
类型?