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.

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路