假设我的项目具有以下层次结构:
fragment/fragment.go
main.go
在fragment.go
我有以下代码,有一个getter,没有setter:
package fragment
type Fragment struct {
number int64 // private variable - lower case
}
func (f *Fragment) GetNumber() *int64 {
return &f.number
}
在 main.go 中,我创建了一个Fragment
并尝试在Fragment.number
没有设置器的情况下进行更改:
package main
import (
"fmt"
"myproject/fragment"
)
func main() {
f := new(fragment.Fragment)
fmt.Println(*f.GetNumber()) // prints 0
//f.number = 8 // error - number is private
p := f.GetNumber()
*p = 4 // works. Now f.number is 4
fmt.Println(*f.GetNumber()) // prints 4
}
所以通过使用指针,我改变了fragment
包外的私有变量。我知道在例如 C 中,指针有助于避免复制大型结构/数组,并且它们应该使您能够更改它们指向的任何内容。但我不太明白它们应该如何处理私有变量。
所以我的问题是:
- 私有变量不应该保持私有,不管它们是如何访问的吗?
- How is this compared to other languages such as C++/Java? Is it the case there too, that private variables can be changed using pointers outside of the class?
My Background: I know a bit C/C++, rather fluent in Python and new to Go. I learn programming as a hobby so don't know much about technical things happening behind the scenes.