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

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

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

问题

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

func abc(client cleintpassedasrefernce) {
    // 在client中进行更改,以便在client的实际内存位置中更新
}

type clienttobepassasref struct {
    data int
    name string
}

// 调用abc函数
object := clienttobepassasref{}
abc(object)

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

type objectPassedStruct struct {
    object *clienttobepassasref
}

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

英文:

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

func abc(client cleintpassedasrefernce) {
    // make changes in client so that it will be updated in the client actual memory location
}


type clienttobepassasref struct {
    data int
    name string
} 

// calling abc function 
object = clienttobepassasref{}
abc(object)

Maybe I can create another struct like type object passed struct

{
    object *clienttobepassasref
}

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

答案1

得分: 1

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

package main

import (
	"fmt"
)

type MyObject struct {
	data int
	name string
}

// 在这里接受一个指向对象的指针,使用'*'字符
func abc(obj *MyObject) {
	// 在客户端中进行更改,以便在客户端的实际内存位置中更新
	obj.name = "foo"
}

func main() {
	// 调用abc函数
	object := MyObject{}
	abc(&object)
	// 在这里通过引用传递,使用'&'字符
	fmt.Println(object.name)
}

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

package main

import (
	"fmt"
)

type MyObject struct {
	data int
	name string
}

// accept a pointer to the object here using the '*' character
func abc(obj *MyObject) {
	// make changes in client so that it will be updated in the client actual memory location
	obj.name = "foo"
}

func main() {
	// calling abc function
	object := MyObject{}
	abc(&object)
	// Pass it by reference here with the '&' character
	fmt.Println(object.name)
}

答案2

得分: 1

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

package main

import "fmt"

type MyObject struct {
	data int
	name string
}

func (obj *MyObject) abc() {
	// 在客户端进行更改,以便在客户端的实际内存位置上进行更新
	obj.name = "some name"
	obj.data = 10
}

func main() {
	// 调用 abc 函数
	object := &MyObject{}
	object.abc()

	fmt.Printf("object = %+v", *object)
	//object = {data:10 name:some name}
}

在此处运行代码:链接

参考获取更多知识:

英文:

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).

package main

import "fmt"

type MyObject struct {
	data int
	name string
}

func (obj *MyObject)abc() {
	// make changes in client so that it will be updated in the client actual memory location
	obj.name = `some name`
	obj.data = 10
}

func main() {
	// calling abc function
	object := &MyObject{}
	object.abc()

	fmt.Printf("object = %+v", *object)
	//object = {data:10 name:some name}
}

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:

确定