Am trying to write a switch case, where switch happens based on the type of structure passed. Am not able to achieve that can anyone please help me out.
The code that i have written is as below,
package main
import (
"fmt"
"reflect"
)
type golan_struct_1 struct {
value_1 int
value_2 string
value_3 byte
}
type golan_struct_2 struct {
value_1 int
value_2 string
value_3 byte
}
type golan_struct_3 struct {
value_1 int
value_2 string
value_3 byte
}
func main() {
formatToString()
}
func formatToString() {
var zzz golan_struct_1
var yyy golan_struct_2
var xxx golan_struct_3
Any(zzz)
Any(yyy)
Any(xxx)
}
func Any(value interface{}) {
formatAtom(reflect.TypeOf(value))
}
func formatAtom(v reflect.Type) {
fmt.Println(v)
//switch v.Kind() {
//case reflect.golan_struct_1:
//fmt.Println("!!! golan_struct_1")
//case reflect.golan_struct_2:
//fmt.Println("!!! golan_struct_2")
//}
//case reflect.golan_struct_3:
//fmt.Println("!!! golan_struct_3")
//}
}
In func formatAtom, am able to print the type of the structure that i received
O/P
main.golan_struct_1
main.golan_struct_2
But my goal is
if the structure is golan_struct_1 it should print !!! golan_struct_1
if the structure is golan_struct_2 it should print !!! golan_struct_2
if the structure is golan_struct_2 it should print !!! golan_struct_3
If i replace the structure instance with structure pointer, it goes to default case, can you please guide me as to what modification i have to do in the above code.
Here i have replaced,
var zzz golan_struct_1
var yyy golan_struct_2
var xx golan_struct_3
with
var zzz *golan_struct_1
var yyy *golan_struct_2
var xx *golan_struct_3
func formatToString() {
var zzz *golan_struct_1
var yyy *golan_struct_2
var xx *golan_struct_3
Any(&zzz)
Any(&yyy)
Any(&xx)
}
EDIT: If i want to edit the content of the structure in func Any(), how can i do that ?
Meaning,
In switch case golan_struct_1: If i want to change the content of the structure or copy the structure content to other structure
i.e
func Any(v interface{}) {
fmt.Println(v)
switch v.(type) {
case golan_struct_1:
fmt.Println("!!! golan_struct_1")
v.value_2 = "hello"
var aaa golan_struct_1
aaa.value_1 = v.value_1
case golan_struct_2:
fmt.Println("!!! golan_struct_2")
case golan_struct_3:
fmt.Println("!!! golan_struct_3")
default:
fmt.Println("!!! default")
}
}
If i do the above i get error, but when i print fmt.Println(v) i get the entire structure content, but not able to access via the variable
Once i switch to golan_struct_1 or golan_struct_1 or golan_struct_1:, i want to access the content of the structure that has been passed and even want to modify the content of the passed structure, so that it gets reflected in called function and also in the switch case
I have added what i want to do as comment in code in func main() and in func Any()
type golan_struct_1 struct {
value_1 int
value_2 string
value_3 byte
}
type golan_struct_2 struct {
value_1 int
value_2 string
value_3 byte
}
type golan_struct_3 struct {
value_1 int
value_2 string
value_3 byte
}
func main() {
var zzz golan_struct_1
zzz.value_1 = 100
zzz.value_2 = "golan_struct_1"
zzz.value_3 = 1
Any(zzz)
fmt.Println(zzz)
// Here i want to see the modified value of structure in func Any
}
func Any(v interface{}) {
switch v.(type) {
case golan_struct_1:
// Here when i get the instance of golan_struct_1 i.e zzz, i want to modify the content
// of structure and also copy individual elements of structure (zzz) to other instance of structure
// var aaa golan_struct_1, i want to copy value_1 of zzz to value_1 of aaa
// Want to modify any value of structure members of golan_struct_1 and want that to be reflected back in
// main()
fmt.Println("!!! golan_struct_1")
case golan_struct_2:
fmt.Println("!!! golan_struct_2")
case golan_struct_3:
fmt.Println("!!! golan_struct_3")
default:
fmt.Println("!!! default")
}
}