相关疑难解决方法(0)

interface {}的含义是什么?

我是接口的新手,并试图通过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)

go

122
推荐指数
5
解决办法
4万
查看次数

为什么Golang需要接口?

在Golang中,我们使用带接收器方法的结构.一切都很完美到这里.
但是,我不确定接口是什么.我们在结构中定义方法,如果我们想在结构上实现一个方法,我们再次在另一个结构下编写它.
这意味着接口似乎只是方法定义,只占用了页面上额外不需要的空间.

有没有例子解释我为什么需要一个界面?

struct interface go

29
推荐指数
4
解决办法
9325
查看次数

在Go中使用接口

我想了解接口类型,并在Go(语言)中使用它的一个简单示例.

我阅读了网络文档,但我没有得到它.

interface go

13
推荐指数
1
解决办法
3609
查看次数

你能解释一下Go Interfaces吗?

我没有得到整个类型+接口模型(用其他语言替换类).如果你能用一种简单的方式解释它们的含义,我们将非常感激.

go

7
推荐指数
2
解决办法
314
查看次数

接口与结构方法

我有带接口的代码:

package main

import (
    "math"
    "fmt"
)

type Circle struct {
    x, y, r float64
}

type Rectangle struct {
    x1, y1, x2, y2 float64
}

type Figure interface {
    Area() float64
}

func (c *Circle) Area() float64 {
    return math.Pi * c.r * c.r
}

func (r *Rectangle) Area() float64 {
    return math.Abs(r.x2 - r.x1) * math.Abs(r.y2 - r.y1)
}

func main() {
    figures := make([]Figure, 0)
    figures = append(figures, &Circle{0, 0, 10})
    figures = append(figures, &Rectangle{0, 0, …
Run Code Online (Sandbox Code Playgroud)

struct interface go

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

Golang推断出接口

这里只是一个简单的语言设计相关问题.在像Swift这样的语言中,为了使struct/class符合协议/接口,需要明确地将其声明为

struct Dog: Animal {
    // implementation of Animal protocols here
}
Run Code Online (Sandbox Code Playgroud)

但是为什么在Go中,没有明确的方法来显示结构符合的接口?

这不仅仅是让事情不清楚,还是有任何其他原因呢?

language-design interface go implements

3
推荐指数
1
解决办法
364
查看次数

如果没有强制执行Go的接口,它们是否必要?

Go语言将接口类型作为功能,类似于C风格的接口.但是,Go的接口类型似乎没有被强制执行 - 它们只是定义协议而不实际应用于类型.由于它们没有强制执行,使用接口仍然是个好主意吗?

go

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

Golang:当你有多个继承权时,接口的重点是什么

我是一名Java程序员,学习在Go中编程.到目前为止,我非常喜欢这种语言.比Java更多.

但有一件事我有点困惑.Java具有接口,因为类只能从一个类继承.由于Go允许多重继承,接口的重点是什么?

java inheritance interface multiple-inheritance go

1
推荐指数
2
解决办法
7589
查看次数

苦苦于看到接口类型的目的

我最近喜欢Go编程语言,到目前为止我发现它很精彩,但我真的很难理解接口.我已经阅读了很多关于它们的内容,但它们对我来说似乎仍然非常抽象.

我写了一些使用以下界面的代码:

package main

import (
  "fmt"
  "math"
)

type Circer interface {
    Circ() float64
}

type Square struct {
    side float64
}

type Circle struct {
    diam, rad float64
}

func (s *Square) Circ() float64 {
    return s.side * 4
}

func (c *Circle) Circ() float64 {
    return c.diam * math.Pi
}

func (c *Circle) Area() float64 {
    if c.rad == 0 {
        var rad = c.diam / 2
        return (rad*rad) * math.Pi
    } else {
        return (c.rad*c.rad) * …
Run Code Online (Sandbox Code Playgroud)

interface go

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

为什么go语言的方法有一个奇怪的语法

我不明白为什么go开发人员选择了类似于func (t Type) MethodName()方法的语法.我无法消化这一事实,特别是在阅读本文并考虑到go是极简主义的事实之后.岂不更简单的语法像func Type.MethodName()func Type::MethodName()已经足够与物体使用隐含参数像访问thisself.或者我错过了当前语法提供的任何优势?

oop go

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