How to pass any type to a function in Go

1 Answer

0 votes
package main

import "fmt"

func printByType(val interface{}) {
    fmt.Printf("%v, %T\n", val, val)
}

func main() {
    printByType(734)        
    printByType("abc") 
    printByType(3.14)        
    printByType("z") 
    printByType('q') 
}
 
 
 
 
/*
run:
     
734, int
abc, string
3.14, float64
z, string
113, int32

*/

 



answered Jun 9, 2023 by avibootz

Related questions

1 answer 80 views
1 answer 151 views
1 answer 207 views
207 views asked Nov 4, 2020 by avibootz
1 answer 181 views
...