问题描述
我发现关于Go。
我试图理解接口的概念,但是我发现从博客文章中的代码片段中很难这样做,并且几乎不可能从。
任何人都可以在工作程序中指出一个Go接口的简单例子吗?
package main
导入fmt;
func Tern(exp bool,一个接口{},b interface {})(interface {}){
if exp {return a}
return b
}
func main(){
a:= 7; b:= 1;
result:= Tern(a> b,a,b);
fmt.Printf(%d \\\
,result);
}
I found an interesting blog post about Go.
I am trying to understand the concept of interfaces, but I find it very hard to do so from the code fragment in the blog post, and nearly impossible from the language specification.
Can anyone point out a simple example of Go's interfaces in a working program?
It's a work-in-progress learning exercise, and certainly a poor example of good style, but here you go (spec).
Additionally, as a more exotic example, I made a post on the go-nuts mailing list regarding using interface{} for building functions that work with anonymous data (in this case, a "ternary operation" function):
package main
import "fmt";
func Tern(exp bool, a interface{}, b interface{}) (interface{}) {
if exp { return a }
return b
}
func main() {
a := 7; b := 1;
result := Tern(a > b, a, b);
fmt.Printf("%d\n", result);
}
这篇关于Go界面的一些例子是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!