Home > Backend Development > Golang > How to build a RESTful API using Golang and deploy to Heroku?

How to build a RESTful API using Golang and deploy to Heroku?

WBOY
Release: 2024-06-01 16:21:02
Original
1008 people have browsed it

How to build a RESTful API using Golang and deploy it to Heroku? Steps: Install Go and create a new project. Write the API code and start the local server. Create a Heroku account and install the Heroku CLI. Initialize Heroku, create the application and deploy the code. Access the deployed application and view the response returned.

如何使用 Golang 构建 RESTful API 并部署到 Heroku?

Build a RESTful API using Golang and deploy to Heroku

Golang is a popular backend programming language known for its high efficiency Known for its ease of use. This tutorial will guide you on how to build a RESTful API using Golang and deploy it to Heroku, a well-known cloud platform.

Build Golang RESTful API

  1. Install Go: Make sure Go is installed in your system. You can download and install the latest version from its official website.
  2. Create a Go project: Create a new Go project in your working directory using the following command:
go mod init restful-api
Copy after login
  1. Create API file: Create a file named main.go in your project directory and enter the following code:
package main

import (
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

func main() {
    router := mux.NewRouter()

    router.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, world!"))
    })

    // 监听端口 8080 的请求
    log.Fatal(http.ListenAndServe(":8080", router))
}
Copy after login
  1. Start API: Run the following command to start your API locally:
go run main.go
Copy after login

You should be able to access it in your browser via http://localhost:8080/hello API.

Deploy to Heroku

  1. Create a Heroku account: Visit the Heroku website and create an account if you haven't already.
  2. Install the Heroku CLI: Follow the instructions in the Heroku documentation to install the Heroku CLI.
  3. Initialize Heroku: Go to your project directory and run the following command:
heroku init
Copy after login
  1. Create the Heroku application: Create a new Heroku application using the following command:
heroku create restful-api
Copy after login
  1. Deploy your application: Push your Go code to the Heroku remote origin:
git push heroku main
Copy after login
  1. Browse your application: Your application will be deployed on Heroku and its URL can be accessed using the Heroku CLI:
heroku open
Copy after login

Practical Case

You can use this RESTful API as the basis for building more complex applications. The following is a practical case showing how to use the API to get data from the database:

func getProducts(w http.ResponseWriter, r *http.Request) {
    db := ... // 数据库连接

    var products []Product

    err := db.Select(&products, "SELECT * FROM products")
    if err != nil {
        log.Println(err)
        http.Error(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
        return
    }

    json.NewEncoder(w).Encode(products)
}
Copy after login

You can also use the API to create, update, and delete data in the database.

The above is the detailed content of How to build a RESTful API using Golang and deploy to Heroku?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
go
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template