dongli4711 2019-06-16 13:33
浏览 65
已采纳

获取“ STRING”变量的INT值

I need output like

0 - os.O_APPEND     - 1024
1 - os.O_CREATE     - 64
2 - os.O_EXCL       - 128
3 - os.O_RDONLY     - 0
4 - os.O_RDWR       - 2
5 - os.O_SYNC       - 1052672
6 - os.O_TRUNC      - 512
7 - os.O_WRONLY     - 1

I am able to to do half of it with

func main() {
        a := []int{os.O_APPEND,os.O_CREATE,os.O_EXCL,os.O_RDONLY,os.O_RDWR,os.O_SYNC,os.O_TRUNC,os.O_WRONLY}
        for index, value := range a {
                fmt.Printf("%d -  - %d
", index, value)
        }
}

which gave me output

0 -  - 1024
1 -  - 64
2 -  - 128
3 -  - 0
4 -  - 2
5 -  - 1052672
6 -  - 512
7 -  - 1

and the other half of it with

func main() {
        a := []string{"os.O_APPEND","os.O_CREATE","os.O_EXCL","os.O_RDONLY","os.O_RDWR","os.O_SYNC","os.O_TRUNC","os.O_WRONLY"}
        for index, value := range a {
                fmt.Printf("%d - %-15s -
", index, value)
        }
}

which gave me the output

0 - os.O_APPEND     -
1 - os.O_CREATE     -
2 - os.O_EXCL       -
3 - os.O_RDONLY     -
4 - os.O_RDWR       -
5 - os.O_SYNC       -
6 - os.O_TRUNC      -
7 - os.O_WRONLY     -

How can I get the desired output ?

Update

As I'm thinking about it, I'm getting an idea for solving it with an array of empty interface and then type asserting on each element of the array of empty interface, once with string to get the string, and once with int to get the value of int, but I don't know how to do that.

  • 写回答

1条回答 默认 最新

  • duanqi5114 2019-06-16 14:03
    关注

    you could use map.

    func main() {
        var m map[string]int
        m = make(map[string]int)
        b := []int{os.O_APPEND,os.O_CREATE,os.O_EXCL,os.O_RDONLY,os.O_RDWR,os.O_SYNC,os.O_TRUNC,os.O_WRONLY}
        a := []string{"os.O_APPEND","os.O_CREATE","os.O_EXCL","os.O_RDONLY","os.O_RDWR","os.O_SYNC","os.O_TRUNC","os.O_WRONLY"}
        for index, value := range a {
            m[value] = b[index]
        }
        var i =0
        for index,mapValue := range m{
            fmt.Println(i," - ",index,"-",mapValue )
            i++
        }
    }
    

    out put will be:

    0  -  os.O_RDWR - 2
    1  -  os.O_SYNC - 1052672
    2  -  os.O_TRUNC - 512
    3  -  os.O_WRONLY - 1
    4  -  os.O_APPEND - 1024
    5  -  os.O_CREATE - 64
    6  -  os.O_EXCL - 128
    7  -  os.O_RDONLY - 0
    

    or you could define custom struct

    type CustomClass struct {
        StringValue string
        IntValue int
    }
    func main() {
    
        CustomArray:=[]CustomClass{
            {"os.O_APPEND",os.O_APPEND},
            {"os.O_CREATE",os.O_CREATE},
            {"os.O_EXCL",os.O_EXCL},
            {"os.O_RDONLY",os.O_RDONLY},
            {"os.O_RDWR",os.O_RDWR},
            {"os.O_SYNC",os.O_SYNC},
            {"os.O_TRUNC",os.O_TRUNC},
            {"os.O_WRONLY",os.O_WRONLY},
        }
        for k, v := range CustomArray {
            fmt.Println(k," - ", v.StringValue," - ", v.IntValue)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接