interface to array golang
func test(t interface{}) {
switch reflect.TypeOf(t).Kind() {
case reflect.Slice:
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
fmt.Println(s.Index(i))
}
}
}
interface to array golang
func test(t interface{}) {
switch reflect.TypeOf(t).Kind() {
case reflect.Slice:
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
fmt.Println(s.Index(i))
}
}
}
array in golang
// Sum function takes a pointer of array with 3 float numbers. if you define `a *[4]float64` then you get type error
func Sum(a *[3]float64) (sum float64) {
for _, v := range *a {
sum += v
}
return
}
array := [...]float64{7.0, 8.5, 9.1}
x := Sum(&array) // Note the explicit address-of operator
/*
## Array in Golang
1. There are major differences between the ways arrays work in
Go and C. In Go:
- Arrays are values. Assigning one array to another copies
all the elements.
- In particular, if you pass an array to a function, it will
receive a copy of the array, not a pointer to it.
- The size of an array is part of its type. The types [10]int
and [20]int are distinct.
2. The value property can be useful but also expensive; if you
want C-like behavior and efficiency, you can pass a pointer
to the array.
*/
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us