Skip to main content
update tour link
Source Link
dskinner
  • 10.9k
  • 3
  • 36
  • 43

Yes, consider some of these examples:

package main import "fmt" // convert types take an int and return a string value. type convert func(int) string // value implements convert, returning x as string. func value(x int) string { return fmt.Sprintf("%v", x) } // quote123 passes 123 to convert func and returns quoted string. func quote123(fn convert) string { return fmt.Sprintf("%q", fn(123)) } func main() { var result string result = value(123) fmt.Println(result) // Output: 123 result = quote123(value) fmt.Println(result) // Output: "123" result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result) // Output: "1111011" foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result) // Output: "foo" _ = convert(foo) // confirm foo satisfies convert at runtime // fails due to argument type // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/XNMtrDUDS0

Tour: https://tour.golang.org/moretypes/21https://tour.golang.org/moretypes/25 (Function Closures)

Yes, consider some of these examples:

package main import "fmt" // convert types take an int and return a string value. type convert func(int) string // value implements convert, returning x as string. func value(x int) string { return fmt.Sprintf("%v", x) } // quote123 passes 123 to convert func and returns quoted string. func quote123(fn convert) string { return fmt.Sprintf("%q", fn(123)) } func main() { var result string result = value(123) fmt.Println(result) // Output: 123 result = quote123(value) fmt.Println(result) // Output: "123" result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result) // Output: "1111011" foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result) // Output: "foo" _ = convert(foo) // confirm foo satisfies convert at runtime // fails due to argument type // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/XNMtrDUDS0

Tour: https://tour.golang.org/moretypes/21 (Function Closures)

Yes, consider some of these examples:

package main import "fmt" // convert types take an int and return a string value. type convert func(int) string // value implements convert, returning x as string. func value(x int) string { return fmt.Sprintf("%v", x) } // quote123 passes 123 to convert func and returns quoted string. func quote123(fn convert) string { return fmt.Sprintf("%q", fn(123)) } func main() { var result string result = value(123) fmt.Println(result) // Output: 123 result = quote123(value) fmt.Println(result) // Output: "123" result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result) // Output: "1111011" foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result) // Output: "foo" _ = convert(foo) // confirm foo satisfies convert at runtime // fails due to argument type // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/XNMtrDUDS0

Tour: https://tour.golang.org/moretypes/25 (Function Closures)

improve example
Source Link
dskinner
  • 10.9k
  • 3
  • 36
  • 43

Yes, consider some of these examples:

package main import ("fmt"  // convert types take "fmt" ) an int and return a string value. type convert func(int) string   // value implements convert, returning x as string. func value(x int) string { return fmt.Sprintf("%v", x) } // quote123 passes 123 to convert func and returns quoted string. func quote123(fn convert) string { return `"` +fmt.Sprintf("%q", fn(123) + `"`) } func main() { var result string   // 123 result = value(123) fmt.Println(result)   // "123"Output: 123  result = quote123(value) fmt.Println(result)   // "1111011"Output: "123"  result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result)   // "foo"Output: "1111011"     foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result)   // confirmsOutput: foo"foo"  satisfies convert at runtime _ = convert(foo) // confirm foo satisfies convert at runtime   // fails due to argument type // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/14AK9qpB3zhttp://play.golang.org/p/XNMtrDUDS0

Tour: https://tour.golang.org/moretypes/21 (Function Closures)

Yes, consider some of these examples:

package main import ( "fmt" )  type convert func(int) string func value(x int) string { return fmt.Sprintf("%v", x) } func quote123(fn convert) string { return `"` + fn(123) + `"` } func main() { var result string   // 123 result = value(123) fmt.Println(result)   // "123" result = quote123(value) fmt.Println(result)   // "1111011" result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result)   // "foo" foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result)   // confirms foo satisfies convert at runtime _ = convert(foo) // fails // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/14AK9qpB3z

Tour: https://tour.golang.org/moretypes/21 (Function Closures)

Yes, consider some of these examples:

package main import "fmt"  // convert types take an int and return a string value. type convert func(int) string   // value implements convert, returning x as string. func value(x int) string { return fmt.Sprintf("%v", x) } // quote123 passes 123 to convert func and returns quoted string. func quote123(fn convert) string { return fmt.Sprintf("%q", fn(123)) } func main() { var result string result = value(123) fmt.Println(result) // Output: 123  result = quote123(value) fmt.Println(result) // Output: "123"  result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result) // Output: "1111011"     foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result) // Output: "foo"  _ = convert(foo) // confirm foo satisfies convert at runtime   // fails due to argument type // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/XNMtrDUDS0

Tour: https://tour.golang.org/moretypes/21 (Function Closures)

Yes, consider some of these examples:

package main import ( "fmt" ) type convert func(int) string func value(x int) string { return fmt.Sprintf("%v", x) } func quote123(fn convert) string { return `"` + fn(123) + `"` } func main() { var result string // 123 result = value(123) fmt.Println(result) // "123" result = quote123(value) fmt.Println(result) // "1111011" result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result) // "foo" foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result) // confirms foo satisfies convert at runtime _ = convert(foo) // fails // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/14AK9qpB3z

Tour: http://tour.golang.org/#45https://tour.golang.org/moretypes/21 (Function Closures)

Yes, consider some of these examples:

package main import ( "fmt" ) type convert func(int) string func value(x int) string { return fmt.Sprintf("%v", x) } func quote123(fn convert) string { return `"` + fn(123) + `"` } func main() { var result string // 123 result = value(123) fmt.Println(result) // "123" result = quote123(value) fmt.Println(result) // "1111011" result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result) // "foo" foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result) // confirms foo satisfies convert at runtime _ = convert(foo) // fails // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/14AK9qpB3z

Tour: http://tour.golang.org/#45 (Function Closures)

Yes, consider some of these examples:

package main import ( "fmt" ) type convert func(int) string func value(x int) string { return fmt.Sprintf("%v", x) } func quote123(fn convert) string { return `"` + fn(123) + `"` } func main() { var result string // 123 result = value(123) fmt.Println(result) // "123" result = quote123(value) fmt.Println(result) // "1111011" result = quote123(func(x int) string { return fmt.Sprintf("%b", x) }) fmt.Println(result) // "foo" foo := func(x int) string { return "foo" } result = quote123(foo) fmt.Println(result) // confirms foo satisfies convert at runtime _ = convert(foo) // fails // _ = convert(func(x float64) string { return "" }) } 

Play: http://play.golang.org/p/14AK9qpB3z

Tour: https://tour.golang.org/moretypes/21 (Function Closures)

improved example, additional links
Source Link
dskinner
  • 10.9k
  • 3
  • 36
  • 43
Loading
Source Link
dskinner
  • 10.9k
  • 3
  • 36
  • 43
Loading