我是接口的新手,并试图通过github做SOAP请求
我不明白的意思
Msg interface{}
Run Code Online (Sandbox Code Playgroud)
在这段代码中:
type Envelope struct {
Body `xml:"soap:"`
}
type Body struct {
Msg interface{}
}
Run Code Online (Sandbox Code Playgroud)
我观察到相同的语法
fmt.Println
Run Code Online (Sandbox Code Playgroud)
但不明白所取得的成就
interface{}
Run Code Online (Sandbox Code Playgroud) 我读过"Effective Go"和其他问答:golang界面兼容性编译类型检查,但是我无法正确理解如何使用这种技术.
请看例子:
type Somether interface {
Method() bool
}
type MyType string
func (mt MyType) Method2() bool {
return true
}
func main() {
val := MyType("hello")
//here I want to get bool if my value implements Somether
_, ok := val.(Somether)
//but val must be interface, hm..what if I want explicit type?
//yes, here is another method:
var _ Iface = (*MyType)(nil)
//but it throws compile error
//it would be great if someone explain the notation …
Run Code Online (Sandbox Code Playgroud) 当我读到docker/distribution源代码的副本时,我发现声明的变量让我很困惑. 代码是:
var _ FileInfo = FileInfoInternal{}
var _ FileInfo = &FileInfoInternal{}
Run Code Online (Sandbox Code Playgroud)
我不知道声明是什么意思,希望得到一些帮助.
我有界面:
type MyInterface interface {
...
}
Run Code Online (Sandbox Code Playgroud)
我想标记我的结构实现它.我认为这是不可能的,但我想确定.
我做了以下,但我认为它会产生一个实现接口的匿名变量.我对吗?
type MyStruct struct {
...
MyInterface
}
Run Code Online (Sandbox Code Playgroud) 我从camlistore看到以下声明(http://code.google.com/p/camlistore/source/browse/pkg/cacher/cacher.go).
var (
_ blobref.StreamingFetcher = (*CachingFetcher)(nil)
_ blobref.SeekFetcher = (*CachingFetcher)(nil)
_ blobref.StreamingFetcher = (*DiskCache)(nil)
_ blobref.SeekFetcher = (*DiskCache)(nil)
)
Run Code Online (Sandbox Code Playgroud)
我知道没有创建变量,并且语句确保编译器检查CachingFether实现StreamingFetcher和SeekFetcher的公共函数.RHS部分使用带有nil参数的指针构造函数语法.这种语法在Go语言中意味着什么?
Effective Go推荐的Interface checks部分
var _ json.Marshaler = (*RawMessage)(nil)
Run Code Online (Sandbox Code Playgroud)
作为RawMessage
实现Marshaler
.
我知道分配如何进行类型检查,但右侧实际上是什么意思?
我目前正在阅读https://github.com/codegangsta/inject go 包的源代码,以了解该包的工作原理。
我对文件https://github.com/codegangsta/inject/blob/master/inject.go文件有一些疑问,该文件使用 Go 语言的某些元素,我不理解,并且在文档中找不到准确的解释。
// InterfaceOf dereferences a pointer to an Interface type.
// It panics if value is not an pointer to an interface.
func InterfaceOf(value interface{}) reflect.Type {
t := reflect.TypeOf(value)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Interface {
panic("Called inject.InterfaceOf with a value that is not a pointer to an interface. (*MyInterface)(nil)")
}
return t
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是关于for
循环的。为什么它使用带有测试表达式的 for 循环?
第二个与恐慌函数中的消息有关。中提到了“指向接口的指针” (*MyInterface)(nil)
。当您检查类型是否实现结构时,我只会在有关“编译时检查结构”的 go 文档中遇到类似的构造:
var …
Run Code Online (Sandbox Code Playgroud) 在Go中,你不\xe2\x80\x99t声明你需要实现一个接口,你只需执行它(它\xe2\x80\x99s称为\xe2\x80\x98结构类型\xe2\x80\x99类似于\xe2 \x80\x98duck 在动态语言中输入 \xe2\x80\x99)。\n如果你想强制一个类型实现一个接口(比如当你 \xe2\x80\x98inherit\xe2\x80\x99 C# 中的接口或例如Java)?换句话说,如果忘记实现接口(或签名错误)是一个错误,并且您希望尽早发现该错误,该怎么办?最好的方法是什么?
\n我有一个定义方法的接口。我有一个实现这个接口的结构。在其中,我实现了该接口中的方法,并且还定义了其他方法。
例如:
package main
import (
"fmt"
)
type Animal interface {
MakeNoise()
}
type Dog struct {
color string
}
/* Interface implementation */
func (d *Dog) MakeNoise() {
fmt.Println("Bark!")
}
/* End Interface implementation */
func (d *Dog) WagTail() {
fmt.Println(d.color + " dog: Wag wag")
}
func NewDog(color string) Animal {
return &Dog{color}
}
func main() {
dog := NewDog("Brown")
dog.MakeNoise()
dog.WagTail()
}
Run Code Online (Sandbox Code Playgroud)
在游乐场上:https ://play.golang.org/p/B1GgoNToNl_l
这里,WagTail() 不是 Animal 接口的一部分,而是属于 Dog 结构。运行此代码会出现错误
dog.WagTail 未定义(Animal 类型没有字段或方法 …
我有一个库,其中有Client
和MockClient
结构,它们都实现相同的ClientInterface
接口。我想编写单元测试来保持这些结构同步,以便它们不仅实现接口,而且具有MockClient
所有的Client
方法。为此,我想获取结构体方法的列表,以便在 中Client
缺少其中一个方法时打印信息丰富的错误消息MockClient
。
我尝试改编How to get the name of a function in Go? 对于这个简化的例子:
package main
import (
"fmt"
"reflect"
"runtime"
)
type Person struct {
Name string
}
func (person *Person) GetName() string {
return person.Name
}
func main() {
person := Person{Name: "John Doe"}
personValue := reflect.ValueOf(&person)
for i := 0; i < personValue.NumMethod(); i++ {
fmt.Println(runtime.FuncForPC(personValue.Method(i).Pointer()).Name())
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的是这个脚本(在https://play.golang.org/p/HwvhEPfWI5I共享)打印GetName
. …
package main
type Writeable interface {
OnWrite() interface{}
}
type Result struct {
Message string
}
func (r *Result) OnWrite() interface{} {
return r.Message
}
// what does this line mean? what is the purpose?
var _ Writeable = (*Result)(nil)
func main() {
}
Run Code Online (Sandbox Code Playgroud)
代码段中的注释表达了我的困惑.据我所知,带注释的行通知编译器检查结构是否已实现接口,但我不太确定.有人可以帮助解释目的吗?
go ×11
interface ×3
docker ×1
go-reflect ×1
methods ×1
oop ×1
pointers ×1
reflection ×1
registry ×1