I am not sure to understand your question.
If you want out
to work only with structs of type DataIP
:
simply define DataIP
outside of main and use the signature func out(k *DataIP)
.
if what you want is to be able to pass any type of structure to out
:
In golang, this sort of generic methods can be implemented using the interface type. As this answer explains, an interface is a container with two words of data:
- one word is used to point to a method table for the value’s underlying type,
- and the other word is used to point to the actual data being held by that value.
An interface can hold anything and is often used as a function parameter to be able to process many sort of inputs.
In your case, you can do:
func out(k interface{}) {
fmt.Println(k)
}
This will print &{Hello! Hello GO!}
. In case you want the &
to disappear (i.e. you always pass it pointers), you can use the reflect
package to "dereference" k
:
func out(k interface{}) {
fmt.Println(reflect.ValueOf(k).Elem())
}
which yields {Hello! Hello GO!}
Here is a playground example.
if what you want is to print the address of Data
:
you can use the %p
pattern with fmt.Printf
:
fmt.Printf("%p", &Data) // 0x1040a130
Using the out
function, you get:
func out(k interface{}) {
fmt.Printf("%p\n", k)
}
See this playground example