如何在Go lang中将数据从控制器传递到表单?

I have a handler / controller that takes in an http request.

func UpdateHandler(request *http.Request) {
    ID := mux.Vars(request)["ID"]
    UpdateForm.Save(ID,db)
}

Then I have a form that I want to process the data and eventually update it.

type UpdateForm struct {
    ID              string            `json:"type"`
}

func (UpdateForm) Save(db mongo.Database) {
    id := ID
    repository.Update(Id)
}

Go will print out undefined ID

How can I make sure that the form gets the value from the controller?

I think it has nothing to do with the handler, but your code isn't consistent. This line

UpdateForm.Save(ID,db)

The method Save() takes two arguments, while the original method signature takes only a single mongo.Database type argument.

Here is what I assume was your intention:

type UpdateForm struct {
    ID     string   `json:"type"`
}

func (u UpdateForm) Save(db mongo.Database) {
    id := u.ID
    repository.Update(id)
}

// UpdateForm instance somewhere
var u = UpdateForm{}

func UpdateHandler(request *http.Request) {
    u.ID := mux.Vars(request)["ID"]
    u.Save(db)
}

You can populate your form using data from the request. If your request contains a JSON encoded body than you could decode it into your form object like this:

package main

import (
    "encoding/json"
    "net/http"
    "strings"
    "fmt"
)

type UpdateForm struct {
    ID string `json:"type"`
}

func main() {
    req, _ := http.NewRequest(
        "POST",
        "http://example.com",
        strings.NewReader(`{"type": "foo"}`),
    )

    var form *UpdateForm
    json.NewDecoder(req.Body).Decode(&form)
    fmt.Println(form.ID) // Output: foo
}

Or you can instantiate it directly like this:

func UpdateHandler(request *http.Request) {
    ID := mux.Vars(request)["ID"]
    form := &UpdateForm{ID: ID}
    form.Save()
}