小编Art*_*zan的帖子

在Go中我可以返回一个符合接口的结构而无法访问该接口吗?

我认为解释这个问题的最好方法就是举例,所以这里是:

package main

import (
    "fmt"
)

// Greeter greets with a Greeting.
type Greeter interface {
    Greet() Greeting
}

// A Greeting has a string representation.
type Greeting interface {
    String() string
}

type Hello struct {}

// Hello greets by returning itself...
func (h *Hello) Greet() *Hello {
    return h
}

// ...because Hello also has a string representation.
func (h *Hello) String() string {
    return "Hello"
}

// But Go says Hello doesn't implement Greeter.
func main() …
Run Code Online (Sandbox Code Playgroud)

interface go

6
推荐指数
1
解决办法
2327
查看次数

标签 统计

go ×1

interface ×1