英文:
accessing struct fields from embedded struct
问题
我想在一个结构体上定义一个方法来验证 HTTP 请求,但是我在访问结构体字段方面遇到了一些问题。
以下是我的代码:
package main
import "log"
type ReqAbstract struct{}
func (r *ReqAbstract) Validate() error {
log.Printf("%+v", r)
return nil
}
func (r *ReqAbstract) Validate2(req interface{}) error {
log.Printf("%+v", req)
return nil
}
type NewPostReq struct {
ReqAbstract
Title string
}
func main() {
request := &NewPostReq{Title: "Example Title"}
request.Validate()
request.Validate2(request)
}
当我运行这段代码时,我得到了以下结果:
2015/07/21 13:59:50 &{}
2015/07/21 13:59:50 &{ReqAbstract:{} Title:Example Title}
有没有办法在 Validate()
方法中像 Validate2()
方法那样访问结构体字段?
英文:
I want to define a method on a struct for validating http request. but I have some problems about accessing struct fields.
there is my code.
package main
import "log"
type ReqAbstract struct{}
func (r *ReqAbstract) Validate() error {
log.Printf("%+v", r)
return nil
}
func (r *ReqAbstract) Validate2(req interface{}) error {
log.Printf("%+v", req)
return nil
}
type NewPostReq struct {
ReqAbstract
Title string
}
func main() {
request := &NewPostReq{Title: "Example Title"}
request.Validate()
request.Validate2(request)
}
When I run this code, I get the below result
2015/07/21 13:59:50 &{}
2015/07/21 13:59:50 &{ReqAbstract:{} Title:Example Title}
is there any way to access struct fields on Validate() method like Validate2() method?
答案1
得分: 8
你不能从内部结构访问外部结构的字段,只能从外部访问内部的字段。你可以使用组合来实现:
type CommonThing struct {
A int
B string
}
func (ct CommonThing) Valid() bool {
return ct.A != 0 && ct.B != ""
}
type TheThing struct {
CommonThing
C float64
}
func (tt TheThing) Valid() bool {
return tt.CommonThing.Valid() && tt.C != 0
}
在这个例子中,TheThing
结构体通过组合 CommonThing
结构体来使用它的字段和方法。TheThing
结构体也可以定义自己的字段和方法。
英文:
You cannot access outer struct fields from inner struct. Only inner fields from the outer. What you can do is composing:
type CommonThing struct {
A int
B string
}
func (ct CommonThing) Valid() bool {
return ct.A != 0 && ct.B != ""
}
type TheThing struct {
CommonThing
C float64
}
func (tt TheThing) Valid() bool {
return tt.CommonThing.Valid() && tt.C != 0
}
答案2
得分: 3
你可以定义一个指向自身的字段。
package main
import (
"log"
)
type ReqAbstract struct {
selfPointer interface{}
}
func (r *ReqAbstract) Assign(i interface{}) {
r.selfPointer = i
}
func (r *ReqAbstract) Validate() error {
log.Printf("%+v", r.selfPointer)
return nil
}
func (r *ReqAbstract) Validate2(req interface{}) error {
log.Printf("%+v", req)
return nil
}
type PostReq struct {
ReqAbstract
Title string
}
func NewPostReq(title string) *PostReq {
pr := &PostReq{Title: title}
pr.Assign(pr)
return pr
}
func main() {
request := NewPostReq("Example Title")
request.Validate()
request.Validate2(request)
}
这将输出:
2009/11/10 23:00:00 &{ReqAbstract:{selfPointer:0x10438180} Title:Example Title}
2009/11/10 23:00:00 &{ReqAbstract:{selfPointer:0x10438180} Title:Example Title}
请查看playground。
英文:
You can define filed with point to himself
package main
import (
"log"
)
type ReqAbstract struct{
selfPointer interface{}
}
func (r *ReqAbstract) Assign(i interface{}) {
r.selfPointer = i
}
func (r *ReqAbstract) Validate() error {
log.Printf("%+v", r.selfPointer)
return nil
}
func (r *ReqAbstract) Validate2(req interface{}) error {
log.Printf("%+v", req)
return nil
}
type PostReq struct {
ReqAbstract
Title string
}
func NewPostReq(title string) *PostReq {
pr := &PostReq{Title:title}
pr.Assign(pr)
return pr
}
func main() {
request := NewPostReq("Example Title")
request.Validate()
request.Validate2(request)
}
This will output:
2009/11/10 23:00:00 &{ReqAbstract:{selfPointer:0x10438180} Title:Example Title}
2009/11/10 23:00:00 &{ReqAbstract:{selfPointer:0x10438180} Title:Example Title}
Check playground
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论