Fórum Root.cz
Hlavní témata => Vývoj => Téma založeno: okalousek 19. 09. 2020, 09:02:11
-
Zdravím. Právě zkouším Golang a začal jsem dělat funkci Contains. Zjistí, jestli je určitý element v poli, avšak chci to udělat generické. Je jedno co bude pole obsahovat, jen se bude porovnávat s hodnotou v argumentech.
package main
import (
"fmt"
"os"
)
func Contains(arr *[]interface{}, value interface{}) bool {
for _, v := range *arr {
if v == value {
return true
}
}
return false
}
func main() {
args := os.Args
fmt.Println(args)
if Contains(&args, "test") {
fmt.Println("Contains test")
}
}
Jenže samozřejmě to nefunguje.
./main.go:19:14: cannot use &args (type *[]string) as type *[]interface {} in argument to Contains
Neví zde někdo jak to udělat? Děkuji
-
Takto:
args := os.Args
var data []interface{}
for _, argument := range args {
data = append(data, argument)
}
if Contains(data, "test") {
println("Contains test")
}
func Contains(data []interface{}, value interface{}) bool {
for _, v := range data {
if v == value {
return true
}
}
return false
}
-
Samozrejme je potreba porovnavat porovnatelne (int versus int, string versus string, apod.), takze pro porovnani int trebas takto, ale porovnavaci funkce je stejna.
integers := []int {1, 2, 3}
var data []interface{}
for _, integer := range integers {
data = append(data, integer)
}
if Contains(data, 1) {
println("Contains 1")
}
-
Tak jeste naposledy
var data []interface{}
integers := []int {1, 2, 3}
for _, integer := range integers {
data = append(data, integer)
}
strings := []string {"jahoda", "malina", "test"}
for _, value := range strings {
data = append(data, value)
}
if Contains(data, 1) {
println("Contains numerical 1")
}
if Contains(data, "test") {
println("Contains string test")
}
-
Řez je hodnotový typ, pole ukazatelů nedává smysl. Po opravě bude fungovat porovnání podle interface{}, jež obsahuje ukazatel na prvek a typ. V případě jiné porovnávací sémantiky by to chtělo rozhraní Equatable.
Také doporučuju kouknout na Go 2, má generika (je ve stavu alfa verze).
-
can you please explain this go syntax to me?
type ImmutableTreeListᐸElementTᐳ struct {
I thought go doesn't have generics.
It doesn't. That's just a "template" file, which I use search and replace in order to generate the three monomorphized go files.
If you look closely, those aren't angle brackets, they're characters from the Canadian Aboriginal Syllabics block, which are allowed in Go identifiers. From Go's perspective, that's just one long identifier.
https://www.reddit.com/r/rust/comments/5penft/parallelizing_enjarify_in_go_and_rust/dcsgk7n/
</trolling>
-
can you please explain this go syntax to me?
type ImmutableTreeListᐸElementTᐳ struct {
I thought go doesn't have generics.
It doesn't. That's just a "template" file, which I use search and replace in order to generate the three monomorphized go files.
If you look closely, those aren't angle brackets, they're characters from the Canadian Aboriginal Syllabics block, which are allowed in Go identifiers. From Go's perspective, that's just one long identifier.
https://www.reddit.com/r/rust/comments/5penft/parallelizing_enjarify_in_go_and_rust/dcsgk7n/
</trolling>
S rustem na mě nemusíte, já ho používám a rád. Ale řekl jsem si že bych mohl zkusit i to Go. V hodně případech mi Rust chyběl, ale jako náhradu za Python kde je třeba výkon dobré.