2021年4月10日星期六

There is a way to create a slice or array that supports all kinds of numbers in Golang to apply very simple functions to it.?

Respected StackOverFlow Community, I am writing to you with the intention of asking for your help in solving my question, or receiving some kind of guidance.

I was thinking in something like :

package main    import "fmt"  import "reflect"    func getMajor(numbers []interface{}) interface{} {      i, flag := 1, len(numbers)      value := numbers[0]      for i < flag {          //But here the code fails in compilation 😢          if reflect.ValueOf(numbers[i]) > reflect.ValueOf(value) {              value = numbers[i]          }          i++      }      return value  }    func main() {      numbers := []interface{}{}        var nuint8 uint8; var nuint16 uint16; var nuint32 uint32; var nuint64 uint64;      var nint8 int8; var nint16 int16; var nint32 int32; var nint64 int64; var nfloat32 float32;      var nfloat64 float64; var ncomplex64 complex64; var ncomplex128 complex128; var nbyte byte;      var nrune rune        numbers = append(numbers, []interface{}{          nuint8, nuint16, nuint32, nuint64, nint8, nint16, nint32,          nint64, nfloat32, nfloat64, ncomplex64, ncomplex128, nbyte,          nrune,      })        fmt.Println(numbers)    }  

The objective is to play with all types of numbers in an slice or array and apply minor methods of sort, find major and find minor.

The GoPlayGround to play is : https://play.golang.org/p/3jhn9ngHID6

I was reading sources like: https://golang.org/pkg/reflect/

And now I know that I can use []interface{}{} to add all types in GOlang but I don't know how to compare values(from interfaces) or how to play with the essence of the interface in a clear way.

thank you very much for your attention and help.

PostData: I am not a gopher and I am not an expert on Golang.

https://stackoverflow.com/questions/67041079/there-is-a-way-to-create-a-slice-or-array-that-supports-all-kinds-of-numbers-in April 11, 2021 at 11:03AM

没有评论:

发表评论