英文:
Resolving Pointers
问题
我对Go(Golang)还有点陌生,指针让我有些困惑。特别是,我似乎无法弄清楚如何解析或取消引用指针。
以下是一个示例:
package main
import "fmt"
type someStruct struct {
propertyOne int
propertyTwo map[string]interface{}
}
func NewSomeStruct() *someStruct {
return &someStruct{
propertyOne: 41,
}
}
func aFunc(aStruct *someStruct) {
aStruct.propertyOne = 987
}
func bFunc(aStructAsValue someStruct) {
// 我希望确保不更改传递给此函数的结构体。
aStructAsValue.propertyOne = 654
}
func main() {
structInstance := NewSomeStruct()
fmt.Println("My Struct:", structInstance)
structInstance.propertyOne = 123 // 如果结构体在另一个包中,我将无法这样做。
fmt.Println("Changed Struct:", structInstance)
fmt.Println("Before aFunc:", structInstance)
aFunc(structInstance)
fmt.Println("After aFunc:", structInstance)
// 我如何将 "structInstance"(类型为 *someStruct)解析/取消引用为
// 类型为 someStruct 的内容,以便将其传递给 bFunc?
// &structInstance 会产生 (类型为 **someStruct)
// 也许我在使用类型断言时有误?
// bFunc(structInstance.(someStruct))
}
在上面的示例中,是否可以使用 structInstance
调用 bFunc
?
如果 someStruct
结构体位于另一个包中,并且由于它是未导出的,那么获取其实例的唯一方法将是通过某个 "New" 函数(假设所有函数都返回指针)。
谢谢。
英文:
I'm a bit new to Go (Golang) and am getting a bit confused by pointers. In particular, I can't seem to figure out how to resolve or dereference a pointer.
The following is an example:
package main
import "fmt"
type someStruct struct {
propertyOne int
propertyTwo map[string]interface{}
}
func NewSomeStruct() *someStruct {
return &someStruct{
propertyOne: 41,
}
}
func aFunc(aStruct *someStruct) {
aStruct.propertyOne = 987
}
func bFunc(aStructAsValue someStruct) {
// I want to make sure that I do not change the struct passed into this function.
aStructAsValue.propertyOne = 654
}
func main() {
structInstance := NewSomeStruct()
fmt.Println("My Struct:", structInstance)
structInstance.propertyOne = 123 // I will NOT be able to do this if the struct was in another package.
fmt.Println("Changed Struct:", structInstance)
fmt.Println("Before aFunc:", structInstance)
aFunc(structInstance)
fmt.Println("After aFunc:", structInstance)
// How can I resolve/dereference "structInstance" (type *someStruct) into
// something of (type someStruct) so that I can pass it into bFunc?
// &structInstance produces (type **someStruct)
// Perhaps I'm using type assertion incorrectly?
//bFunc(structInstance.(someStruct))
}
Code on "Go Playground"
http://play.golang.org/p/FlTh7_cuUb
In the example above, is it possible to call "bFunc" with "structInstance"?
This could potentially be even more of an issue if the "someStruct" struct is in another package and since it's unexported, the only way to get an instance of it will be through some "New" function (which, let's say will all return pointers).
Thanks.
答案1
得分: 3
你在这里混淆了两个问题,这与指针无关,而与导出的变量有关。
type someStruct struct {
propertyOne int
propertyTwo map[string]interface{}
}
someStruct
、propertyOne
和propertyTwo
并没有被导出(它们不以大写字母开头),所以即使你在另一个包中使用NewSomeStruct
,也无法访问这些字段。
至于bFunc
,你可以在变量名前面加上*
来解引用指针,例如:
bFunc(*structInstance)
我强烈建议你阅读《Effective Go》,特别是《指针 vs. 值》一节。
英文:
You're confusing 2 issues here, this has nothing to do with pointers, it has to do with exported variables.
type someStruct struct {
propertyOne int
propertyTwo map[string]interface{}
}
someStruct
, propertyOne
and propertyTwo
aren't exported (they don't start with a capital letter), so even if you use NewSomeStruct
from another package, you wouldn't be able to access the fields.
And about bFunc
, you dereference a pointer by appending *
before the variable name, for example:
bFunc(*structInstance)
I highly recommend going through Effective Go, specially the Pointers vs. Values section.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论