Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Go中使用闭包时如何从函数类型返回_Go - Fatal编程技术网

在Go中使用闭包时如何从函数类型返回

在Go中使用闭包时如何从函数类型返回,go,Go,我是刚来GoLang的新手,无法找出下面代码中的错误。我想做的是“动态创建并返回打印机函数” 我得到以下编译错误 cannot use createPrintFunction("!!!") (type Printer) as type func(string) in argument to Greet2 问题是签名不匹配。这包括返回类型 Greet2想要func(string)但是createPrintFunction返回func(string)string。请注意,Greet2不返回任何内

我是刚来GoLang的新手,无法找出下面代码中的错误。我想做的是“动态创建并返回打印机函数”

我得到以下编译错误

 cannot use createPrintFunction("!!!") (type Printer) as type func(string) in argument to Greet2

问题是签名不匹配。这包括返回类型

Greet2
想要
func(string)
但是
createPrintFunction
返回
func(string)string
。请注意,Greet2不返回任何内容,但
createPrintFunction
函数返回一个字符串

你必须决定如何使他们匹配。您可能希望使用
Greet2
中的返回值来获得比“完成”更好的结果


既然您已经定义了
type Printer func(s string)string
,最好一致地使用它来避免此类问题
Greet2
使用
打印机
createPrintFunction
返回
打印机
。然后,如果
打印机
发生更改,则仍能正常工作

func Greet2(salutation Salutation, do Printer) string {
    return do(salutation.name + " " + salutation.greeting)
}
…但是由
createPrintFunction
生成的
do
不使用其参数。很难建议它应该做什么,因为这段代码中的大部分只是直接传递参数,或者忽略它们

无论如何,签名必须匹配。

解决方案:

func Greet2(salutation Salutation, do Printer) (string){
    return do(salutation.name + " " + salutation.greeting)
}

type Printer func(string) string

func createPrintFunction(custom string) Printer {
    return func(s string) string {
        return s
    }
}

func pluralSightModule1Closures1(w http.ResponseWriter, r *http.Request) {
    var s = Salutation{"Joe", "Hello"}
    res := Greet2(s, createPrintFunction("!!!"))
    w.Header().Set("Content-Type", "application/json")
    w.Write([]byte (res))

}

请显示
Greet2
@adrian Added Greet2的定义
Greet2
func(string)
作为其第二个参数,而不是
Printer
,后者是
func(s string)字符串。类型不同,也不可互换。我已将打印机类型更改为func(string)
type打印机func(string)string
。相同的
错误
。另外,
createPrintFunction(!!!)
func(string)
的一种类型,因此类型错误没有意义。不,
createPrintFunction(!!!)
返回一个
Printer
,它是
func(string)
字符串,与
func(string)
不同,后者是
Greet2
所采用的。回报值显著
Greet2
接受一个没有返回值的参数,而
Printer
的返回值类型为
string
。这正是我在评论中写的。@Adrian这是一个问答网站。你本来可以写个答案的。¯_(ツ)_/“@Schwern如果你阅读了评论,你会发现需要反复阅读才能找到答案。@Adrian你投入工作很好,但没有得到答案。该网站的目的是产生好的答案,可以投票、编辑和评论,这样人们就不必费力地阅读评论。没有答案。
func Greet2(salutation Salutation, do Printer) string {
    return do(salutation.name + " " + salutation.greeting)
}
func Greet2(salutation Salutation, do Printer) (string){
    return do(salutation.name + " " + salutation.greeting)
}

type Printer func(string) string

func createPrintFunction(custom string) Printer {
    return func(s string) string {
        return s
    }
}

func pluralSightModule1Closures1(w http.ResponseWriter, r *http.Request) {
    var s = Salutation{"Joe", "Hello"}
    res := Greet2(s, createPrintFunction("!!!"))
    w.Header().Set("Content-Type", "application/json")
    w.Write([]byte (res))