How to return from function types when using closures in Go

huangapple go评论64阅读模式
英文:

How to return from function types when using closures in Go

问题

我是你的中文翻译助手,以下是代码的翻译:

package main

import (
	"fmt"
	"net/http"
)

type Salutation struct {
	name     string
	greeting string
}

type Printer func(s string) string

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

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

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))
}

func main() {
	http.HandleFunc("/", pluralSightModule1Closures1)
	http.ListenAndServe(":8080", nil)
}

我得到以下编译错误:

无法将 createPrintFunction("!!!")(类型为 Printer)作为 Greet2 的参数 func(string) 类型使用

请注意,我已经将代码包装在一个 main 包中,并添加了一个 main 函数和一个简单的 HTTP 服务器来运行代码。如果你只是想编译代码而不是运行一个 HTTP 服务器,你可以忽略 main 函数和与 HTTP 相关的部分。

英文:

Im new to GoLang and cannot figure out whats wrong in the code below. What i'm trying to do is "Create and return Printer function on the fly"

type Salutation struct {  name string	
    greeting string
}

type Printer func(s string) string

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

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

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))
}

I get the following compilation error

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

答案1

得分: 3

问题在于签名不匹配,包括返回类型。

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

你需要决定如何使它们匹配。你可能希望在 Greet2 中使用返回值,而不仅仅是 "Done"。

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

由于你定义了 type Printer func(s string) string,最好始终一致地使用它,以避免这种问题。Greet2 接受一个 Printer,而 createPrintFunction 返回一个 Printer。这样,即使 Printer 发生变化,代码仍然可以正常工作。

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

...但是 createPrintFunction 生成的 do 函数没有使用它的参数。很难建议它应该做什么,因为这段代码中的大部分都是直接传递参数或忽略它们。

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

英文:

The problem is the signatures don't match. That includes the return type.

Greet2 wants func(string) but createPrintFunction returns func(s string) string. Note that Greet2's returns nothing, but the createPrintFunction function returns a string.

You'll have to decide how to make them match. You probably want to use the return value in Greet2 for something better than "Done".

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

Since you've defined type Printer func(s string) string its best to use it consistently to avoid this sort of problem. Greet2 takes a Printer and createPrintFunction returns a Printer. Then if Printer changes things will still work.

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

...but the do generated by createPrintFunction doesn't use its arguments. It's hard to advise what it should do because much of this code just passes its arguments straight through, or ignores them.

Anyhow, the signatures have to match.

答案2

得分: -1

解决方案:

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))
}

以上是要翻译的内容。

英文:

Solution:

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))

}

huangapple
  • 本文由 发表于 2017年6月13日 01:35:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/44505515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定