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.
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)
}
}