doxd96148 2018-10-06 08:07
浏览 36
已采纳

使用指针和值接收器构造类型的实例

This question already has an answer here:

Lets take this very small example, having a function that modify value inside the struct:

package learn

type Point struct {
    x int
    y int
}

func (p *Point) Set(x int, y int) {
    p.x = x
    p.y = y
}

this works properly, used like this for instance:

package main

import (
    "NewPattern/learn"
    "fmt"
)

func main() {
    p := learn.Point{}
    p.Set(5, 6)
    fmt.Print(p)
}

it outputs the expected value: {5,6}

Now let's say I don't want the user having a constructor, I can change the code by adding this function:

func NewPoint(x int, y int) Point {
    return Point{x, y}
}

then I can use in main like this:

func main() {
    p := learn.NewPoint(3, 8)
    fmt.Print(p)
    p.Set(5, 6)
    fmt.Print(p)
}

and it works as expected returning {3 8}{5 6}.

Well now we want to prevent creating point without calling the constructor - not really the case here, but can make sense for complex classes - so we avoid exporting Point and we create an interface instead, so I refactored the code like this: (this is not working!)

package learn

type point struct {
    x int
    y int
}

type Point interface {
    Set(x int, y int)
}

func (p *point) Set(x int, y int) {
    p.x = x
    p.y = y
}

func NewPoint(x int, y int) Point {
    return point{x, y} //error here
}

This says:

cannot use point literal (type point) as type Point in return argument:
    point does not implement Point (Set method has pointer receiver)

I can "fix" this by modifying the method in:

func NewPoint(x int, y int) point {
    return point{x, y}
}

but this just move the error in main, that is refactored like:

func main() {
    var p learn.Point
    p = learn.NewPoint(3, 8) //error here!
    fmt.Print(p)
    p.Set(5, 6)
    fmt.Print(p)
}

and the error is:

cannot use learn.NewPoint(3, 8) (type learn.point) as type learn.Point in assignment:
    learn.point does not implement learn.Point (Set method has pointer receiver)

by googling I managed to solve in this way:

func NewPoint(x int, y int) *point {
    return &point{x, y}
}

but as a result in the main we are obtaining: &{3 8}&{5 6} as a print, ad as well I don't get actually what is happening behind the scenes.

I guess this is somehow related by having things passed and maybe "returned" by value, is this the case? But I don't know how the first examples without interface worked without effort. Can please someone clarify these details that I think are essential to Go understanding.

</div>
  • 写回答

1条回答 默认 最新

  • duanbin3021 2018-10-06 08:22
    关注

    point and *point (i.e the pointer to point) are two different types. In your code the interface Point is implemented by *point type. You can implement the constructor as:

    func NewPoint(x int, y int) Point {
        return &point{x, y} 
    }
    

    The printing will show & befor the points value as the underlying value is a pointer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?