Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

html/template: How to bind nested structs from form data?

Aperta
#2,746 1 commento 5 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Attiva
Stack tecnologico
go
Ambito
api, backend

Direzione di ricerca

Parti dalla chiamata a c.Bind nell'esempio ed esamina come il repository gestisce il parsing dei form delle richieste e dei valori annidati. Confronta questo comportamento con le convenzioni della libreria standard per i dati dei form, quindi determina se la convenzione di denominazione prevista è supportata e se il risultato richiede documentazione o una proposta di funzionalità circoscritta.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I'm trying to bind form data to nested structs, but I'm not sure if I'm doing it correctly. Is there a recommended way to handle form binding with nested structs using the standard library?

My Use Case
I have a form structure with nested fields that I want to bind to corresponding nested structs:

type UserInfo struct {
    Name string   `form:"name"`
    Sex string   `form:"sex"`
}

type UserGroup struct {
    Men []UserInfo `form:"men"`
    Women   []UserInfo `form:"women"`
}

type UserResults struct {
    Win          UserGroup `form:"win"`
    Lose UserGroup `form:"lose"`
}

func handler(w http.ResponseWriter, r *http.Request) {
    form := &UserResults{}
    if err := c.Bind(form); err != nil {
	// error handle
    }
    // Values are not bound to nested structs
}

What I've Tried
I've attempted to use dot notation in the form field names:

<form method="POST">
    <input type="number" name="win.men[0].name" value="john">
    <input type="number" name="win.men[0].sex" value="1">
</form>

Questions

  1. Is there a built-in way to handle nested struct binding with the standard library?
  2. If yes, what is the correct naming convention for form fields?
  3. Is there any documentation I might have missed about this topic?

Environment

  • Go version: 1.22

Any guidance or best practices would be greatly appreciated. If this functionality isn't currently supported, would it be worth considering as a feature request?

Lingua principale
Go
Stelle
32.7k
Fork
2.8k
Merge medio
9h 39m
PR unite (30g)
6

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di labstack/echo

Tutte le issue di labstack/echo

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.