英文:
How do I pass a pointer to a structure in a function?
问题
我想知道如何用什么来替换*Type?结构体在内部的地址是什么?
//mycode.go
package main
import "fmt"
func out(k *Type) {
    fmt.Println(k)
}
func main() {
    type DataIP struct{ Title, Desc string }
    Data := DataIP{
        "Hello!",
        "Hello GO!",
    }
    out(&Data)
}
我想知道如何用什么来替换*Type?结构体在内部的地址是什么?
英文:
I wonder how to replace *Type  by ? What address has the structure inside?
//mycode.go
package main
import "fmt"
func out(k *Type) {
    fmt.Println(k)
}
func main() {
    type DataIP struct{ Title, Desc string }
    Data := DataIP{
        "Hello!",
        "Hello GO!",
    }
    out(&Data)
}
答案1
得分: 1
我不确定是否理解你的问题。
如果你希望out函数只能用于DataIP类型的结构体:
只需在main函数外定义DataIP,并使用func out(k *DataIP)的函数签名。
如果你希望能够将任何类型的结构体传递给out函数:
在Go语言中,可以使用接口类型来实现这种类型的通用方法。正如这个答案所解释的那样,接口是一个包含两个数据字的容器:
- 一个字用于指向值的底层类型的方法表,
 - 另一个字用于指向该值所持有的实际数据。
 
接口可以持有任何类型的值,并且通常用作函数参数,以便能够处理多种类型的输入。
在你的情况下,你可以这样做:
func out(k interface{}) {
    fmt.Println(k)
}
这将打印出&{Hello! Hello GO!}。如果你希望&消失(即你总是传递指针),你可以使用reflect包来"取消引用" k:
func out(k interface{}) {
    fmt.Println(reflect.ValueOf(k).Elem())
}
这将输出{Hello! Hello GO!}。
这里有一个示例供你参考。
如果你想打印Data的地址:
你可以使用fmt.Printf的%p格式:
fmt.Printf("%p", &Data) // 0x1040a130
使用out函数,你可以这样做:
func out(k interface{}) {
    fmt.Printf("%p\n", k)
}
参考这个示例。
英文:
I am not sure to understand your question.
If you want out to work only with structs of type DataIP:
simply define DataIP outside of main and use the signature func out(k *DataIP).
if what you want is to be able to pass any type of structure to out:
In golang, this sort of generic methods can be implemented using the interface type. As this answer explains, an interface is a container with two words of data:
- one word is used to point to a method table for the value’s underlying type,
 - and the other word is used to point to the actual data being held by that value.
 
An interface can hold anything and is often used as a function parameter to be able to process many sort of inputs.
In your case, you can do:
func out(k interface{}) {
    fmt.Println(k)
}
This will print &{Hello! Hello GO!}. In case you want the & to disappear (i.e. you always pass it pointers), you can use the reflect package to "dereference" k:
func out(k interface{}) {
    fmt.Println(reflect.ValueOf(k).Elem())
}
which yields {Hello! Hello GO!}
Here is a playground example.
if what you want is to print the address of Data:
you can use the %p pattern with fmt.Printf:
fmt.Printf("%p", &Data) // 0x1040a130
Using the out function, you get:
func out(k interface{}) {
    fmt.Printf("%p\n", k)
}
See this playground example
答案2
得分: 1
你需要在main()之外定义类型DataIP,以便该类型在包的范围内而不仅仅在main函数内部:
package main
import "fmt"
type DataIP struct{ Title, Desc string }
func out(k *DataIP) {
    fmt.Println(k)
}
func main() {
    Data := DataIP{
        "Hello!",
        "Hello GO!",
    }
    out(&Data)
}
链接:https://play.golang.org/p/cUS6ttcUy-
英文:
You need to define the type DataIP outside of main() that the type is in the scope of the package and not just inside of the main function:
package main
import "fmt"
type DataIP struct{ Title, Desc string }
func out(k *DataIP) {
	fmt.Println(k)
}
func main() {
	Data := DataIP{
		"Hello!",
		"Hello GO!",
	}
	out(&Data)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论