2

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\n", 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 -\n", 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.

GypsyCosmonaut
  • 661
  • 12
  • 17
  • You can’t do that in Go. See https://stackoverflow.com/questions/24836696/how-to-get-the-variable-name-as-in-the-source-code-using-reflect?rq=1 – georgeok Jun 16 '19 at 23:15

1 Answers1

3

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)
    }
}
Ali Zohrevand
  • 393
  • 4
  • 20
  • Thanks but naahh.. I don't want to declare two arrays of different types, I could have done that, but that means repeating yourself. Any better solution ? – GypsyCosmonaut Jun 16 '19 at 14:09
  • 2
    @GypsyCosmonaut: you could use a `map[string]int` instead. – Sergio Tulentsev Jun 16 '19 at 15:18
  • @SergioTulentsev You also mean like Ali, i.e, using like `map[string]int{"os.O_RDWR" : os.RDWR, "os.O_SYNC" : os.O_SYNC}` . This is a repeating pattern. And as I've come to understand from others and is usually said in coding, repeating is bad habit. I am just trying not to repeat. – GypsyCosmonaut Jun 17 '19 at 02:27
  • it is not possible, because in string array you insert the value of cell statically, you could define struct which has two attributes. – Ali Zohrevand Jun 17 '19 at 07:17
  • custom struct solution added. – Ali Zohrevand Jun 17 '19 at 07:24
  • 1
    @GypsyCosmonaut: you're misinterpreting what DRY means. It does not mean "there must be no duplication in your code, none whatsoever, for any reason". It means "if you need to change behaviour of your code, there should be just one place that knows/controls that piece of logic". In my proposed example with a map, the knowledge (correspondence between name of a setting and its value) is reasonably contained/grouped. Yes, you'd have to type the names twice, but that's a price I'd be willing to pay to avoid messing with runtime/reflection. – Sergio Tulentsev Jun 17 '19 at 09:07