How to make variable type slices - go

I was learning go by doing some of the problems I have already completed in other languages.
So one of the problem was:-
Find the sum of the elements in Array
Arr = [1,2,[7,8,3],1,[3,[2,[4,5]],6]]
I have tried making an interface type array but that also didn't work
func ProdSum(prods interface{},sum int ,depth int){
for id,val:=range prods{
if isArray(val){
ProdSum(val,sum,depth)
}
}
}
type arr []interface{}
func main(){
arra:=arr{5,2,arr{7,-1},3,arr{6,arr{-13,8},4}}
ProdSum(arra,0,1)
}
Error :
./prodsum.go:16:14: cannot range over prods (type interface {})

Use a type assertion or type switch to determine if a value is a slice or an integer. Here's how to do it using a type switch:
func sum(value interface{}) int {
switch value := value.(type) {
case arr:
var result int
for _, v := range value {
result += sum(v)
}
return result
case int:
return value
default:
panic("type not handled")
}
}
Call it like this:
a := arr{5, 2, arr{7, -1}, 3, arr{6, arr{-13, 8}, 4}}
n := sum(a)
Run it on the playground.

The way you are declaring and initializing the array is correct, although you don't need the arr type.
Without the arr type it will look like this:
Running in Go Playground
myArr := []interface{}{1, 2, []int{7, 8, 3}, 1, []interface{}{3, []interface{}{2, []int{4, 5}}, 6}}
Now, the specific error you are seeing is not related to your slice declaration. It is related to the function signature of ProdSum.
ProdSum takes an argument of type interface{}- which is not a slice so you cannot iterate over it. You want the argument to have a type of []interface{} (slice of empty interface):
func ProdSum(prods []interface{}, sum int, depth int) {
for itr, prod := range prods {
// prod has type: interface{}
}
}
Keep in mind that you will need to do type assertions when reading the values out of the slice and summing them. You can't add an interface{} to anything because (before an assertion to int) Go doesn't recognize it as a number.

Related

go 1.18 generic compile error when use maps.Copy on map with struct key

I implemented a Set based on generic, and everything ok until i use struct as Set element instead of base type. I got an compliation error.
go version: go version go1.18 windows/amd64
Below code is failed to complie in function AddSet.
package main
import (
"fmt"
"golang.org/x/exp/maps"
)
type Key struct {
A, B int
}
func main() {
s := SetOf(
Key{1, 1},
Key{2, 2},
Key{3, 3},
)
s.AddSet(SetOf(
Key{3, 3},
Key{4, 4},
Key{5, 5},
))
fmt.Println(s)
}
type Set[T comparable] map[T]struct{}
func SetOf[T comparable](vs ...T) Set[T] {
s := Set[T]{}
for _, v := range vs {
s[v] = struct{}{}
}
return s
}
func (s Set[T]) AddSet(another Set[T]) {
maps.Copy(s, another)
}
when run it:
> go run .\main.go
# command-line-arguments
.\main.go:19:10: cannot use &.autotmp_29 (type *struct { A int; B int }) as type *Key in argument to runtime.mapassign
<autogenerated>:1: cannot use &.autotmp_12 (type *struct { A int; B int }) as type *Key in argument to runtime.mapassign
if Key only has 1 field, it can be compiled successful.
if i use for v := range another { s[v]=struct{}{} }, it can be compiled successful.
i think it's strange, can someone explain please?
It looks like this compiler error. It is fixed in Go 1.19 and backported to Go 1.18.2.
If you are on an older version, I'd recommend simply forgoing the maps package and doing things by hand, as you already tried. It's just a simple loop:
func (s Set[T]) AddSet(another Set[T]) {
for k := range another {
s[k] = struct{}{}
}
}
#icza's comment of explicitly converting the named map type to its underlying type also works:
maps.Copy(map[T]struct{}(s), another)
In case you use functions that expect more than one map type parameter (with the same constraints), as maps.Equal or maps.EqualFunc, you have to convert both arguments:
func (s Set[T]) Compare(another Set[T]) bool {
// signature is Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool
return maps.Equal(map[T]struct{}(s), map[T]struct{}(another))
}
It seems the crash was reproduced also with parametrized map types instantiated with arrays with len >= 2.

Remove an interface item from a slice

I want to remove an item in a slice w/o having to use a specific function for every type of items in the slice. So, I am using interface{} as the slice item type:
package main
import "fmt"
func sliceRemoveItem(slice []interface{}, s int) []interface{} {
return append(slice[:s], slice[s+1:]...)
}
func main() {
array := []int{1,2,3,4,5,6,7}
fmt.Println(array)
fmt.Println(sliceRemoveItem(array,1))
}
But goLang doesn't like it:
./prog.go:13:30: cannot use array (type []int) as type []interface {} in argument to sliceRemoveItem
https://play.golang.org/p/wUrR5iGRZ5Y
Any idea how to do this? Is it possible to use a generic single function accepting any type of slice items?
Refs: How to delete an element from a Slice in Golang
You're trying to pass a slice of int as a slice of interface{}. Go doesn't do this conversion implicitly since it is a costly operation.
Check out this answer: https://stackoverflow.com/a/12754757
You can either accept []interface{}, but do the conversion explicitly, or specify the type as []int. This works:
package main
import "fmt"
func sliceRemoveItem(slice []int, s int) []int {
return append(slice[:s], slice[s+1:]...)
}
func main() {
array := []int{1,2,3,4,5,6,7}
fmt.Println(array)
fmt.Println(sliceRemoveItem(array,1))
}
Use the reflect package.
// sliceRemoveItem removes item at index i from the
// slice pointed to by slicep.
func sliceRemoveItem(slicep interface{}, i int) {
v := reflect.ValueOf(slicep).Elem()
v.Set(reflect.AppendSlice(v.Slice(0, i), v.Slice(i+1, v.Len())))
}
Call it like this:
slice := []int{1, 2, 3, 4, 5, 6, 7}
sliceRemoveItem(&slice, 1)
To avoid type assertions in the caller, the function uses a pointer to slice argument.
Run it on the Go playground

How to delete different type of slice using only 1 methods

I have 2 go functions like following
func removeL2McEntry(a []api.L2McEntry, index int) []api.L2McEntry {
a = append(a[:index], a[index+1:]...)
element
return a[:len(a)]
}
func removeVlagBasedGroup(a []api.VlanPortBased, index int) []api.VlanPortBased {
a = append(a[:index], a[index+1:]...)
return a[:len(a)]
}
As you can see, both functions are doing the same work. But I need to separate them because the outputs and the inputs of the functions are different type.
I have tried:
func removeSlice(a interface{}, idx int) interface{} {
switch v := a.(type) {
case []string:
v = append(v[:idx], v[idx+1:]...)
fmt.Println("is ary", v)
return v[:len(v)]
case []int:
v = append(v[:idx], v[idx+1:]...)
fmt.Println("is ary", v)
return v[:len(v)]
default:
}
return nil
}
But there is too many repetitive code in this way.
Is there any way to make it just one function and reduce the repetitive code?
Thanks in advance.
As Adrian noted, removing an element from a slice is one line of code, in general:
a = append(a[:i], a[i+1]...)
// or
a = a[:i+copy(a[i:], a[i+1:])]
It's not really worth writing a function for it, just use this code snippet where needed.
If you do need to create a function that can handle any slice types, it can be created using reflection. But when using it, you will have to use a type assertion on the result, as the function can only return a static type of interface{}. It will also be slower than using the above snippet on your concrete slice value!
The above remove steps can be "reproduced" using the reflect package. Slicing is the Value.Slice() method, and the append operation is the reflect.AppendSlice() function.
This is how it could look like (types and bound checks omitted):
func remove(s interface{}, i int) interface{} {
v := reflect.ValueOf(s)
return reflect.AppendSlice(v.Slice(0, i), v.Slice(i+1, v.Len())).Interface()
}
Testing it:
is := []int{0, 1, 2, 3}
is = remove(is, 2).([]int)
fmt.Printf("%#v\n", is)
ss := []string{"0", "1", "2", "3"}
ss = remove(ss, 2).([]string)
fmt.Printf("%#v\n", ss)
Output (try it on the Go Playground):
[]int{0, 1, 3}
[]string{"0", "1", "3"}
But again: I don't advise anyone to use this (although working) code, just remove the element directly with the original snippet.

Build array with different types

I'm very new in Go, and I came from Ruby. So...
can I build array what includes different types
[1, 2, "apple", true]
Sorry if it's stupid question.
Thanks.
You can do this by making a slice of interface{} type. For example:
func main() {
arr := []interface{}{1, 2, "apple", true}
fmt.Println(arr)
// however, now you need to use type assertion access elements
i := arr[0].(int)
fmt.Printf("i: %d, i type: %T\n", i, i)
s := arr[2].(string)
fmt.Printf("b: %s, i type: %T\n", s, s)
}
Read more about this here.
Depending on the situation, you might be able to use a struct instead:
package main
import "fmt"
type array struct {
one, two int
fruit string
truth bool
}
func main() {
arr := array{1, 2, "apple", true}
fmt.Println(arr)
}
https://golang.org/ref/spec#Struct_types

Initialize multiple values in a struct using one function

I would like to initialize multiple variables in a struct using the same function like so:
type temp struct {
i int
k int
}
func newtemp(age int) *temp{
return &temp{
i, k := initializer(age)
}
}
func initializer(age int)(int, int){
return age * 2, age * 3
}
however, I can not due to having to use : to initialize variables when creating a struct, is there any way I can do something that is valid yet like the code above?
Using composite literal you can't.
Using tuple assignment you can:
func newtemp(age int) *temp{
t := temp{}
t.i, t.k = initializer(age)
return &t
}
Testing it:
p := newtemp(2)
fmt.Println(p)
Output (try it on the Go Playground):
&{4 6}

Resources