Skip to main content

开始使用 GO

¥Getting Started with GO

🧹 删除了集成指南

Strapi 文档团队专注于改进 Strapi 5 核心体验的文档。我们将在未来 6 个月内发布许多更改,请密切关注👀。

¥The Strapi Documentation team focuses on improving the documentation for Strapi 5's core experience. We will release many changes in the next 6 months, so please keep an eye out 👀.

因此,当前页面现在仅处于维护模式,可能未完全与 Strapi 5 保持同步,并且很快将从 docs.strapi.io 中删除并移至 Strapi 的 集成页面

¥As a result, the present page is now in maintenance mode only, might not be fully up-to-date with Strapi 5, and will soon be removed from docs.strapi.io and moved to Strapi's integrations page.

同时,如果你想帮助我们改进此页面,请随时 contribute 到文档的 GitHub 存储库。

¥In the meantime, if you want to help us improve this page, please feel free to contribute to the documentation's GitHub repository.

本集成指南遵循 快速入门指南。你应该能够通过浏览 URL http://localhost:1337/api/restaurants 使用该 API。

¥This integration guide follows the Quick Start Guide. You should be able to consume the API by browsing the URL http://localhost:1337/api/restaurants.

如果你尚未阅读快速入门指南,则使用 GO 请求 Strapi API 的方式将保持不变,只是你不获取相同的内容。

¥If you haven't gone through the Quick Start guide, the way you request a Strapi API with GO remains the same except that you do not fetch the same content.

创建一个 Go 文件

¥Create a Go file

确保你的计算机上有 Go 已安装

¥Be sure to have Go installed on your computer.

touch strapi.go

Go 有内置的包来发出 HTTP 请求,例如 GET、POST、PUT 和 DELETE。我们将使用 "net/http" 包和其他包。

¥Go has built-in packages to make HTTP Requests like GET, POST, PUT, and DELETE. We will use the "net/http" package along with other packages.

GET 请求你的集合类型

¥GET Request your collection type

restaurant 集合类型执行 GET 请求以获取所有餐厅。

¥Execute a GET request on the restaurant collection type in order to fetch all your restaurants.

确保你激活了 restaurant 集合类型的 find 权限。

¥Be sure that you activated the find permission for the restaurant collection type.

Example GET request
response, error := http.Get("http://localhost:1337/api/restaurants")
Example response
[{
"id": 1,
"name": "Biscotte Restaurant",
"description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.",
"created_at": "2021-03-31T11:37:16.964Z",
"updated_at": "2021-03-31T11:37:16.975Z",
"categories": [{
"id": 1,
"name": "French Food",
"created_by": 1,
"updated_by": 1,
"created_at": "2021-03-31T11:36:23.164Z",
"updated_at": "2021-03-31T11:36:23.172Z"
},
{
"id": 2,
"name": "Brunch ",
"published_at": "2021-03-07T10:10:19.608Z",
"created_at": "2021-03-07T10:10:14.477Z",
"updated_at": "2021-03-07T10:10:19.649Z"
}
]
}]

示例

¥Example

package main

import (
"fmt"
"io/ioutil"
"net/http"
)

func main() {
getD()
}

func getD() {
fmt.Println("Getting data...")
res, error := http.Get("http://localhost:1337/api/restaurants")
if error != nil {
fmt.Printf("The HTTP request failed with error %s\n", error)
} else {
data, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(data))
}
}

POST 请求你的集合类型

¥POST Request your collection type

restaurant 集合类型执行 POST 请求以创建餐厅。

¥Execute a POST request on the restaurant collection type in order to create a restaurant.

确保你激活了 restaurant 集合类型的 create 权限和 category 集合类型的 find 权限。

¥Be sure that you activated the create permission for the restaurant collection type and the find permission for the category Collection type.

Example POST request
postRest, _ := json.Marshal(map[string]string{
"name": "Nwanyi Igbo",
"description": "This is a very nice place to eat native soup",
})
responseBody := bytes.NewBuffer(postRest)
resp, error := http.Post("http://localhost:1337/api/restaurants", "application/json", responseBody)
Example response
{
"id": 2,
"name": "Nwanyi Igbo",
"description": "This is a very nice place to eat native soup",
"created_at": "2021-03-04T09:57:11.669Z",
"updated_at": "2021-04-04T09:57:11.669Z",
"categories": []
}

示例

¥Example

package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)

func main() {
postD()
}

func getD() {
fmt.Println("Getting data...")
resp, error := http.Get("http://localhost:1337/api/restaurants")
if error != nil {
fmt.Printf("The HTTP request failed with error %s\n", error)
} else {
data, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(data))
}
}

func postD() {
fmt.Println("Posting data...")
//Encode the data
postRest, _ := json.Marshal(map[string]string{
"name": "Nwanyi Igbo",
"description": "This is a very nice place to eat native soup",
})
responseBody := bytes.NewBuffer(postRest)
resp, error := http.Post("http://localhost:1337/api/restaurants", "application/json", responseBody)
//Handle Error
if error != nil {
log.Fatalf("An Error Occurred %v", error)
}
defer resp.Body.Close()
//Read the response body
body, error := ioutil.ReadAll(resp.Body)
if error != nil {
log.Fatalln(error)
}
fmt.Println(string(body))
}

PUT 请求你的集合类型

¥PUT Request your collection type

restaurant 集合类型执行 PUT 请求以更新餐厅的类别。

¥Execute a PUT request on the restaurant collection type in order to update the category of a restaurant.

确保你激活了 restaurant 集合类型的 update 权限。PUT 请求略有不同,因为我们需要针对我们想要更新的特定内容。为此,我们首先向 http://localhost:1337/api/restaurants/1 发出请求,然后更新我们想要更新的内容。在此示例中,我们要将 "比斯科特餐厅" 更新为 "餐厅首页"。

¥Be sure that you activated the update permission for the restaurant collection type. PUT Request is sligtly different as we need to target the particular thing we want update. We do this by first making a request to http://localhost:1337/api/restaurants/1 and then update what we want to update. In this example, we are going to update "Biscotte Restaurant" to "Restaurant Home".

Example PUT request
putRest, _ := json.Marshal(map[string]string {
"name": "Restaurant Homes",
})
client := &http.Client{}
url := "http://localhost:1337/api/restaurants/1"
req, error := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(putRest))
req.Header.Set("Content-Type", "application/json")
Example response
{
"id": 1,
"name": "Restaurant Homes",
"description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.",
"published_at": "2021-03-07T10:10:46.949Z",
"created_at": "2021-03-07T10:08:53.929Z",
"updated_at": "2021-03-07T14:06:01.567Z",
"categories": [{
"id": 1,
"name": "French Food",
"published_at": "2021-03-07T10:09:43.232Z",
"created_at": "2021-03-07T10:09:34.135Z",
"updated_at": "2021-03-07T10:09:43.280Z"
},
{
"id": 2,
"name": "Brunch ",
"published_at": "2021-03-07T10:10:19.608Z",
"created_at": "2021-03-07T10:10:14.477Z",
"updated_at": "2021-03-07T10:10:19.649Z"
}
]
}

示例

¥Example

package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)

func main() {
//getD()
//postD()
putD()
}
func getD() {
fmt.Println("Getting data...")
resp, error := http.Get("http://localhost:1337/api/restaurants")
if error != nil {
fmt.Printf("The HTTP request failed with error %s\n", error)
} else {
data, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(data))
}
}
func postD() {
fmt.Println("Posting data...")

// Encode the data
postRest, _ := json.Marshal(map[string]string{
"name": "Nwanyi Igbo",
"description": "This is a very nice place to eat native soup",
})

responseBody := bytes.NewBuffer(postRest)
resp, error := http.Post("http://localhost:1337/api/restaurants", "application/json", responseBody)
// Handle Error
if error != nil {
log.Fatalf("An Error Occured %v", error)
}
defer resp.Body.Close()
// Read the response body
body, error := ioutil.ReadAll(resp.Body)
if error != nil {
log.Fatalln(error)
}

fmt.Println(string(body))
}
func putD() {
putRest, _ := json.Marshal(map[string]string{
"name": "Restaurant Homes",
})
client := &http.Client{}
url := "http://localhost:1337/api/restaurants/1"
req, error := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(putRest))
req.Header.Set("Content-Type", "application/json")
if error != nil {
log.Fatal(error)
}
resp, error := client.Do(req)
if error != nil {
log.Fatal(error)
}
defer resp.Body.Close()

body, error := ioutil.ReadAll(resp.Body)
if error != nil {
log.Fatal(error)
}
fmt.Println(string(body))
}