我正在看这篇文章: https://travix.io/encapsulating-dependencies-in-go-b0fd74021f5a
它提到了这个例子:
package external
type FileReader struct {
Path string
}
func NewFileReader(path string) *FileReader {
return &FileReader{
Path: path,
}
}
func (f *FileReader) ReadTheFile() (string, error) {
// Implementation kept simple on purpose
return "reading a file", nil
}
以上内容与此不同:
package external
type FileReader struct {
Path string
}
func NewFileReader(path string) FileReader {
return FileReader{
Path: path,
}
}
func (f FileReader) ReadTheFile() (string, error) {
// Implementation kept simple on purpose
return "reading a file", nil
}
答案 0 :(得分:4)
&
获取变量的内存地址,*
指向内存地址的值。
a := 12
b = &a
*b++
&a返回a的内存地址,并且* b ++递增1到a的值。
阅读Dave cheny的博客文章,其中介绍了go中的指针: Understand Go pointers in less than 800 words or your money back