Home >Backend Development >Golang >How to Access Struct Fields from an Embedded Struct in Go?

How to Access Struct Fields from an Embedded Struct in Go?

Susan Sarandon
Susan SarandonOriginal
2024-10-31 22:58:011055browse

How to Access Struct Fields from an Embedded Struct in Go?

Accessing Struct Fields from Embedded Struct

When defining methods on a struct to validate HTTP requests, you may encounter issues accessing fields from an embedded struct.

Consider the following code:

<code class="go">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</code>

The above is the detailed content of How to Access Struct Fields from an Embedded Struct in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn