Answers for "golang interface to int"

1

golang convert interface to concrete type

type Person struct {
	firstName string
	lastName  string
}
func printIfPerson(object interface{}) {
	person, ok := object.(Person)
	if ok {
		fmt.Printf("Hello %s!\n", person.firstName)
	}
}
Posted by: Guest on May-26-2020
0

golang interface to int

var myInt interface{}
myInt = 8

toInt, ok := myInt.(int)
fmt.Println(toInt, ok)  // 8 true

toString, ok := myInt.(string)
fmt.Println(toString, ok)  // "" false
Posted by: Guest on May-17-2021
0

interface to int golang

iAreaId := val.(int)
iAreaId, ok := val.(int) // Alt. non panicking version
Posted by: Guest on October-12-2020

Browse Popular Code Answers by Language