How to Access Fields of Embedded Structures in Go Validation Methods?

Mary-Kate Olsen
Release: 2024-11-03 20:43:29
Original
1027 people have browsed it

How to Access Fields of Embedded Structures in Go Validation Methods?

Accessing Struct Fields in Embedded Structures

In Go, defining methods on structs for validation purposes is common. However, accessing fields of embedded structures within those methods can be challenging.

Let's examine the provided 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>
Copy after login

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!