Golang code to switch based on different structure type

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 you do

fmt.Println(v.Kind())

you will see that all your structs have the Kind struct. So this approach will not work.

Try this instead:

package main

import (
	"fmt"
)

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(value)
}

func formatAtom(v interface{}) {

	switch v.(type) {
	case golan_struct_1:
		fmt.Println("!!! golan_struct_1")
	case golan_struct_2:
		fmt.Println("!!! golan_struct_2")
	case golan_struct_3:
		fmt.Println("!!! golan_struct_3")
	}
}

Note the switch on v.(type) and that reflect is not used at all.

1 Like

.oO(Which part of the forum software is responsible for replacing three ‘x’ chars with ■■■?!)

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

There is not much to change. Just make sure your pointers are not nil but initialised to point to something:

func formatToString() {
	zzz := golan_struct_1{}
	yyy := golan_struct_2{}
	xxx := golan_struct_3{}
	Any(zzz)
	Any(yyy)
	Any(xxx)
}

func Any(value interface{}) {
	formatAtom(value)
}

func formatAtom(v interface{}) {
	switch v.(type) {
	case golan_struct_1:
		fmt.Println("!!! golan_struct_1")
	case golan_struct_2:
		fmt.Println("!!! golan_struct_2")
	case golan_struct_3:
		fmt.Println("!!! golan_struct_3")
	}
}

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

What do you want to print instead?

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

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.