好吧,因此,鑑於您告訴我的情況以及您的示例所包含的信息,我可能會這樣做。
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type S3 struct {
Accesskeyid string
Secretaccesskey string
Bucket string
}
type Test struct {
Outputformat string
Wait string
Output S3
File []byte
}
func main() {
myStrut := Test{
Outputformat: "Json",
Wait: "Some Time",
Output: S3{
Accesskeyid: "my id",
Secretaccesskey: "secret id",
Bucket: "east",
},
File: []byte(`some bytes`),
}
jsonValue, err := json.Marshal(myStrut)
if err != nil {
panic(err)
}
fmt.Printf("Test showing that data was marshalled\n %q\n", jsonValue)
resp, err := http.Post("some url", "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
}
從我在你可能也無法打開該文件作爲一個字節數組分配給你的結構的意見閃閃發光
現在。下面是一個示例,您可以使用它幫助您將文件作爲字節數組打開,以便可以將這些字節分配給您的結構。
package main
import (
"fmt"
"io/ioutil"
)
func main() {
//An example of how to open a file and turn it into bytes for your struct
byteArray, err := ioutil.ReadFile("input.txt")
if err != nil {
panic(err)
}
fmt.Println(byteArray)
}
你能更具體一點嗎?看着你的代碼,我發現你的結構沒有錯。輸出是一種S3,所以你已經創建了一個嵌套的對象。如果文件被轉換成一個字節數組,那麼字段File應該沒有問題接受它。所以我想你能給出關於這個問題的更多細節,錯誤消息等。 – reticentroot
@reticentroot是的,我的結構是正確的,但我在哪裏使用它們?我如何將它們發送到POST請求? –
看看這個堆棧,接受的答案http://stackoverflow.com/questions/24455147/how-do-i-send-a-json-string-in-a-post-request-in-go它告訴你如何做一個帖子,你可能需要使用json軟件包編組你的數據。 – reticentroot