<分区>
我想在 expend 中使用 upCase 作为变量,但它说“不能将‘upCase(s1)’(type string)用作 type func(string) string。 我怎样才能转换 upCase 类型?或者我需要做什么才能使错误消失?
package main
import (
"fmt"
"strings"
)
func main() {
s := "test func"
s1 := ""
test := expand(s, upCase(s1))
fmt.Print(test)
}
func expand(s string, f func(string) string) string {
s1 := "test"
var s2 string
if strings.Contains(s, s1) {
// todo 字符串中s1的值更改为f(s1)的值
s1 = f(s1)
strings.Replace(s1, s, s2, -1)
} else {
return ""
}
return s2
}
func upCase(s string) string {
s = strings.ToUpper(s)
return s
}