在函数参数中不使用*structobject将结构体按引用传递

huangapple go评论113阅读模式
英文:

Passing a struct by reference without passing as a *structobject in the function parameters

问题

我需要将一个结构体作为引用传递给一个函数,但是我不能在函数参数中使用指针(*)语法:

  1. func abc(client cleintpassedasrefernce) {
  2. // 在client中进行更改,以便在client的实际内存位置中更新
  3. }
  4. type clienttobepassasref struct {
  5. data int
  6. name string
  7. }
  8. // 调用abc函数
  9. object := clienttobepassasref{}
  10. abc(object)

也许我可以创建另一个结构体,像这样传递对象:

  1. type objectPassedStruct struct {
  2. object *clienttobepassasref
  3. }

然后在函数中稍后访问它?

英文:

I need to pass a struct as a reference to a function; however, I cannot use pointer (*) syntax in my function parameters:

  1. func abc(client cleintpassedasrefernce) {
  2. // make changes in client so that it will be updated in the client actual memory location
  3. }
  4. type clienttobepassasref struct {
  5. data int
  6. name string
  7. }
  8. // calling abc function
  9. object = clienttobepassasref{}
  10. abc(object)

Maybe I can create another struct like type object passed struct

  1. {
  2. object *clienttobepassasref
  3. }

and pass this object and access it later in the function?

答案1

得分: 1

你需要同时更改函数签名和调用函数的方式。此外,你定义了一个名为clienttobepassasref的结构体类型,但是你的函数接受的是一个名为cleintpassedasrefernce的结构体类型,在你的示例中没有定义。我在我的示例中将它们替换为一个名为MyObject的结构体类型。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type MyObject struct {
  6. data int
  7. name string
  8. }
  9. // 在这里接受一个指向对象的指针,使用'*'字符
  10. func abc(obj *MyObject) {
  11. // 在客户端中进行更改,以便在客户端的实际内存位置中更新
  12. obj.name = "foo"
  13. }
  14. func main() {
  15. // 调用abc函数
  16. object := MyObject{}
  17. abc(&object)
  18. // 在这里通过引用传递,使用'&'字符
  19. fmt.Println(object.name)
  20. }

go playground

英文:

You need to change both the function signature, and the method by which you're calling the function. Also you're defining a struct type called clienttobepassasref but your function is accepting a struct type of cleintpassedasrefernce which is not defined in your example. I replaced these with a struct type in my example called MyObject.

go playground

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type MyObject struct {
  6. data int
  7. name string
  8. }
  9. // accept a pointer to the object here using the '*' character
  10. func abc(obj *MyObject) {
  11. // make changes in client so that it will be updated in the client actual memory location
  12. obj.name = "foo"
  13. }
  14. func main() {
  15. // calling abc function
  16. object := MyObject{}
  17. abc(&object)
  18. // Pass it by reference here with the '&' character
  19. fmt.Println(object.name)
  20. }

答案2

得分: 1

如果您需要更新或更改结构的属性,可以创建一个带有该结构引用的接收器函数(方法),并在该函数(方法)中更新结构的属性。

  1. package main
  2. import "fmt"
  3. type MyObject struct {
  4. data int
  5. name string
  6. }
  7. func (obj *MyObject) abc() {
  8. // 在客户端进行更改,以便在客户端的实际内存位置上进行更新
  9. obj.name = "some name"
  10. obj.data = 10
  11. }
  12. func main() {
  13. // 调用 abc 函数
  14. object := &MyObject{}
  15. object.abc()
  16. fmt.Printf("object = %+v", *object)
  17. //object = {data:10 name:some name}
  18. }

在此处运行代码:链接

参考获取更多知识:

英文:

If you need to update, change struct's properties, then create a function (method) with receiver of that struct reference and update struct's properties in that function (method).

  1. package main
  2. import "fmt"
  3. type MyObject struct {
  4. data int
  5. name string
  6. }
  7. func (obj *MyObject)abc() {
  8. // make changes in client so that it will be updated in the client actual memory location
  9. obj.name = `some name`
  10. obj.data = 10
  11. }
  12. func main() {
  13. // calling abc function
  14. object := &MyObject{}
  15. object.abc()
  16. fmt.Printf("object = %+v", *object)
  17. //object = {data:10 name:some name}
  18. }

run the code here

reference to get some more knowledge

huangapple
  • 本文由 发表于 2021年7月6日 01:54:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/68260302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定