如何将自定义类型转换interface{}为基本类型(例如uint8)?
我不能用直接铸造的喜欢uint16(val.(Year)),因为我可能不知道所有的自定义类型,但是我能确定的基本类型(uint8,uint32在运行时,...)
有许多基于数字的自定义类型(通常用作枚举):
例如:
type Year uint16
type Day uint8
type Month uint8
Run Code Online (Sandbox Code Playgroud)
等等...
问题是从类型转换interface{}到基类型:
package main
import "fmt"
type Year uint16
// ....
//Many others custom types based on uint8
func AsUint16(val interface{}) uint16 {
return val.(uint16) //FAIL: cannot convert val (type interface {}) to type uint16: need type assertion
}
func AsUint16_2(val interface{}) uint16 {
return uint16(val) //FAIL: cannot convert val (type interface {}) to type …Run Code Online (Sandbox Code Playgroud) 如何将字典作为函数的参数列表传递,如在Go 3中的Python 3中?
Python 3:
def bar(a,b,c):
print(a,b,c)
args={c: 1, b: 2, a: 3}
bar(**args)
Run Code Online (Sandbox Code Playgroud)
空白:
func bar( a, b, c int) {
fmt.Printf("%d, %d, %d", a, b, c)
}
func main(){
args := make(map[string]int)
args["a"] = 3
args["b"] = 2
args["c"] = 1
// what next ?
}
Run Code Online (Sandbox Code Playgroud) 我可以在使用时以编程方式从共享库(仅限Linux)获取所有函数名称的列表dl_open()吗?
我想要这样的东西:
std::vector<std::string> list_all_functions(void *dl) {
//... what can I do here?
}
int main() {
void * dl = dl_open("./mylib.so", RTLD_NOW);
auto functions = list_all_functions(dl);
//...
dl_close(dl);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
示例库(mylib.so)
标题(.h):
extern "C" {
int sum (int a, int b);
}
Run Code Online (Sandbox Code Playgroud)
来源(.c):
int sum (int a, int b) { return a + b; }
Run Code Online (Sandbox Code Playgroud)
我知道的肮脏的黑客:使用nm或objdump实用
我可以SET通过Redis的引擎在Redis中保存客户端IP 吗?
像这样的东西:
SET my_key $client_ip
Run Code Online (Sandbox Code Playgroud)