小编Ale*_*Bar的帖子

键入自定义类型到基类型

如何将自定义类型转换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

19
推荐指数
2
解决办法
1万
查看次数

去命名参数

如何将字典作为函数的参数列表传递,如在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)

reflection go python-3.x

13
推荐指数
2
解决办法
6337
查看次数

以编程方式在共享库中获取函数名称

我可以在使用时以编程方式从共享库(仅限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)

我知道的肮脏的黑客:使用nmobjdump实用

c c++ linux shared-libraries dlopen

11
推荐指数
2
解决办法
1332
查看次数

有没有办法在Redis中存储客户端IP?

我可以SET通过Redis的引擎在Redis中保存客户端IP 吗?

像这样的东西:

SET my_key $client_ip
Run Code Online (Sandbox Code Playgroud)

redis

4
推荐指数
1
解决办法
186
查看次数

标签 统计

go ×2

c ×1

c++ ×1

dlopen ×1

linux ×1

python-3.x ×1

redis ×1

reflection ×1

shared-libraries ×1