Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何Copy非匿名嵌套结构体 #211

Open
Flipped199 opened this issue May 9, 2024 · 2 comments
Open

如何Copy非匿名嵌套结构体 #211

Flipped199 opened this issue May 9, 2024 · 2 comments
Assignees

Comments

@Flipped199
Copy link

如何Copy非匿名嵌套结构体

type A struct {
	Address    string 
}

type Project struct {
	Name string 
}

type AWithProject struct {
	A A  
	Project     Project 
}

type C struct{
        Address string
        Project     struct{
            Name string
        }
}

//  以上的情况从AWithProject 到 C只能成功复制Project的内容,无法复制Address这个字段,尝试过添加tag也无法生效
@Flipped199
Copy link
Author

在C中使用匿名嵌套A的方式可以解决以上问题,貌似也属于是曲线救国了,要是库能原生支持就好了,或者使用tag支持xx.xx的嵌套层级,这样可以更方便的实现一些功能

@go-kenka
Copy link

不是有一种Method到Field的写法吗?
我觉得你可以添加一个Address方法就可以实现了。

func (a * AWithProject) Address() string {
	return a.A.Address
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants