Error: cannot define new methods on non-local type []string #21802
-
Hi, I'd like to concatenate all elements in fn mapconcat(arr []string) string {
mut ret := ''
for s in arr {
ret += s
}
return ret
}
arr := ['aaa', 'bbb']
s := mapconcat(arr)
println(s) // =>aaabbb Okey. However, I prefer to use it as a method if possible. fn (arr []string) mapconcat() string {
mut ret := ''
for s in arr {
ret += s
}
return ret
}
arr := ['aaa', 'bbb']
s := arr.map(|x| x.to_upper()).mapconcat()
println(s) But I got an error:
I have no idea though it looks like Golang's receiver error relatives. Does anyone have how to fix? |
Beta Was this translation helpful? Give feedback.
Answered by
Delta456
Jul 4, 2024
Replies: 1 comment 2 replies
-
You need to make an alias type Alias = []string
fn (arr Alias) mapconcat() string {
mut ret := ''
for s in arr {
ret += s
}
return ret
}
arr := Alias(['aaa', 'bbb'])
s := Alias(arr.map(|x| x.to_upper())).mapconcat()
println(s) |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
hidsh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to make an alias