Fórum Root.cz

Hlavní témata => Vývoj => Téma založeno: okalousek 19. 09. 2020, 09:02:11

Název: Jak obejít absenci generik v Golangu
Přispěvatel: 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.

Kód: [Vybrat]
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
Název: Re:Jak obejít absenci generik v Golangu
Přispěvatel: Petr Jahoda 19. 09. 2020, 09:56:01
Takto:

Kód: [Vybrat]
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
}
Název: Re:Jak obejít absenci generik v Golangu
Přispěvatel: Petr Jahoda 19. 09. 2020, 10:04:40
Samozrejme je potreba porovnavat porovnatelne (int versus int, string versus string, apod.), takze pro porovnani int trebas takto, ale porovnavaci funkce je stejna.

Kód: [Vybrat]
integers := []int {1, 2, 3}
var data []interface{}
for _, integer := range integers {
data = append(data, integer)
}
if Contains(data, 1) {
println("Contains 1")

}
Název: Re:Jak obejít absenci generik v Golangu
Přispěvatel: Petr Jahoda 19. 09. 2020, 10:10:02
Tak jeste naposledy

Kód: [Vybrat]
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")
}
Název: Re:Jak obejít absenci generik v Golangu
Přispěvatel: Idris 19. 09. 2020, 14:43:30
Ř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).
Název: Re:Jak obejít absenci generik v Golangu
Přispěvatel: _Jenda 19. 09. 2020, 21:18:32
Citace
Citace
can you please explain this go syntax to me?

Kód: [Vybrat]
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>
Název: Re:Jak obejít absenci generik v Golangu
Přispěvatel: okalousek 19. 09. 2020, 23:37:06
Citace
Citace
can you please explain this go syntax to me?

Kód: [Vybrat]
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é.