file, _ := os.Open("/path/to/file") defer file.Close() body := &bytes.Buffer{} writer := multipart.NewWriter(body) writer.WriteField("username", "johndoe") part, _ := writer.CreateFormFile("file", filepath.Base(file.Name())) io.Copy(part, file) writer.Close()
values := map[string]string{ "username": "johndoe", "password": "s3cr3t", } body := &bytes.Buffer{} writer := multipart.NewWriter(body) for key, val := range values { _ = writer.WriteField(key, val) } writer.Close()In this example, a map of field names and values is used to populate a multipart message. WriteField is called for each key-value pair in the map to create a separate part in the message. Overall, these examples utilize the WriteField method of the go mime.multipart package to add simple fields to MIME multipart messages.