Home Backend Development Golang How is RESTful API implemented in Go language?

How is RESTful API implemented in Go language?

Jun 10, 2023 pm 06:40 PM
go language Method to realize restful api

With the development of the Internet and mobile applications, RESTful API has become a popular interface design style. Because it is simple, scalable and efficient, more and more companies and developers are choosing RESTful API as the interface for their applications. In the Go language, it is very common to implement RESTful APIs. This article will introduce the implementation of RESTful API in Go language and some best practices.

1. What is RESTful API?

RESTful API is a Web service interface based on REST (Representational State Transfer) architecture. It uses the HTTP protocol for communication and implements CRUD (create, read, update, delete) operations on server resources through various HTTP methods. These HTTP methods include GET, POST, PUT, DELETE, etc. RESTful API emphasizes the identification of resources and state conversion. Each resource has a unique URI (Uniform Resource Identifier) ​​to identify the resource, and uses HTTP methods to convert its state.

2. Basic steps to implement RESTful API in Go language

1. Install necessary libraries

To implement RESTful API in Go language, you need to use some common packages, such as net/http, encoding/json, etc. Installing these libraries is very simple, just run the following command in the terminal:

go get -u github.com/gorilla/mux
go get -u github.com/rs/cors
Copy after login

Among them, the gorilla/mux library is one of the most popular HTTP request routers in the Go language, which provides powerful HTTP routing functions. The rs/cors library allows you to easily handle cross-origin resource sharing (CORS).

2. Create a RESTful API route

In the Go language, we can use the mux package to create a RESTful API route. First, we need to import the mux package and then define a new router. Next, we can use the router's HandleFunc method to map different HTTP methods and routing paths to handler functions. For example, the following is a simple example of implementing a RESTful API:

package main

import (
 "encoding/json"
 "log"
 "net/http"
 "github.com/gorilla/mux"
 "github.com/rs/cors"
)

type Book struct {
 ID    string  `json:"id"`
 Title string  `json:"title"`
}

var books []Book

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

 // Get all books
 router.HandleFunc("/books", getBooks).Methods("GET")

 // Get a book by ID
 router.HandleFunc("/books/{id}", getBook).Methods("GET")

 // Add a book
 router.HandleFunc("/books", addBook).Methods("POST")

 // Update a book
 router.HandleFunc("/books/{id}", updateBook).Methods("PUT")

 // Delete a book
 router.HandleFunc("/books/{id}", deleteBook).Methods("DELETE")

 handler := cors.Default().Handler(router)

 log.Fatal(http.ListenAndServe(":8080", handler))
}

func getBooks(w http.ResponseWriter, r *http.Request) {
 json.NewEncoder(w).Encode(books)
}

func getBook(w http.ResponseWriter, r *http.Request) {
 params := mux.Vars(r)
 for _, item := range books {
  if item.ID == params["id"] {
   json.NewEncoder(w).Encode(item)
   return
  }
 }
 json.NewEncoder(w).Encode(&Book{})
}

func addBook(w http.ResponseWriter, r *http.Request) {
 var book Book
 _ = json.NewDecoder(r.Body).Decode(&book)
 books = append(books, book)
 json.NewEncoder(w).Encode(book)
}

func updateBook(w http.ResponseWriter, r *http.Request) {
 params := mux.Vars(r)
 for index, item := range books {
  if item.ID == params["id"] {
   books[index].Title = params["title"]
   json.NewEncoder(w).Encode(books[index])
   return
  }
 }
 json.NewEncoder(w).Encode(books)
}

func deleteBook(w http.ResponseWriter, r *http.Request) {
 params := mux.Vars(r)
 for index, item := range books {
  if item.ID == params["id"] {
   books = append(books[:index], books[index+1:]...)
   break
  }
 }
 json.NewEncoder(w).Encode(books)
}
Copy after login

In the above code, we define a Book structure and a books array. We use mux.NewRouter() to create a new router and use the router.HandleFunc() method to map HTTP methods and routing paths to RESTful API handler functions. For example, the function getBooks() handles the GET /books request and encodes the books array via json.NewEncoder() and writes it to the ResponseWriter.

In the main() function, we also use the cors.Default().Handler() method to create a new CORS handler, and use the http.ListenAndServe() method to start the RESTful API service and listen to the default port 8080.

3. Use the transfer object pattern (DTO)

In the design of RESTful API, we need to define the transfer object (DTO), which is a simple data structure used on the client Transfer data to and from the server. In Go language, we can use structure (struct) as DTO. Each structure represents a type of data and contains fields to be sent to the client. For example, the following code defines a User structure:

type User struct {
 ID       int    `json:"id"`
 Name     string `json:"name"`
 Email    string `json:"email"`
 Password string `json:"-"`
}
Copy after login

In the above code, we define a User structure, which contains the ID, Name, Email and Password fields. We use the json tag to convert the structure fields into JSON format. Note that we use the "-" flag to ignore the Password field to avoid passing the password in clear text to the client.

4. Use HTTPS to protect RESTful API

Since RESTful API communicates through HTTP protocol, its security may be affected. In order to ensure the security of RESTful API, we can use HTTPS (HTTP Secure) protocol to protect the communication content. The HTTPS protocol uses SSL (Secure Socket Layer) or TLS (Transport Layer Security) encryption technology to ensure the security of HTTP requests and responses.

In the Go language, we can use the net/http package and crypto/tls package to support HTTPS. For example, the following code demonstrates how to create an HTTPS server with a self-signed certificate:

package main

import (
 "log"
 "net/http"
 "crypto/tls"
)

func main() {
 mux := http.NewServeMux()

 // Define HTTP routes

 // Create self-signed certificate
 cert, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
 if err != nil {
  log.Fatal(err)
 }

 // Create HTTPS server
 server := &http.Server{
  Addr: ":8443",
  TLSConfig: &tls.Config{
   Certificates: []tls.Certificate{cert},
  },
 }

 // Start HTTPS server
 log.Fatal(server.ListenAndServeTLS("", ""))
}
Copy after login

In the above code, we use http.NewServeMux() to create a new HTTP router and then use tls.LoadX509KeyPair() Load the self-signed certificate. Finally, we start the HTTPS server using the http.Server.ListenAndServeTLS() method and pass the certificate configuration to TLSConfig.

5. Use JWT to protect RESTful API

When developing a RESTful API, we also need to protect the API to ensure that only authorized users can access it. A common approach is to use JWT (JSON Web Token) for authentication and authorization.

In the Go language, we can use the jwt-go package to generate and verify JWT. For example, the following code demonstrates how to create and verify a JWT using the jwt-go package:

package main

import (
 "fmt"
 "time"
 "github.com/dgrijalva/jwt-go"
)

func main() {
 // Define a claim
 claim := jwt.MapClaims{
  "user_id": 1234,
  "iss": "myapp",
  "exp": time.Now().Add(time.Hour * 24).Unix(),
 }

 // Create a new JWT
 token := jwt.NewWithClaims(jwt.SigningMethodHS256, claim)

 // Sign the JWT using a secret key
 secret := []byte("mysecret")
 signedToken, err := token.SignedString(secret)
 if err != nil {
  fmt.Println(err)
 }

 fmt.Println("JWT:", signedToken)

 // Verify the JWT
 token, err = jwt.Parse(signedToken, func(token *jwt.Token) (interface{}, error) {
  if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
   return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
  }

  return secret, nil
 })

 if err != nil {
  fmt.Println(err)
 }

 if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
  fmt.Println("JWT claims:", claims)
 } else {
  fmt.Println("Invalid JWT")
 }
}
Copy after login

In the above code, we have defined a MapClaims variable as a declaration. This corresponds to the JSON payload of the JWT, which can contain arbitrary key-value pairs. We set the expiration time of the JWT to 24 hours. We then create a new JWT using the jwt.NewWithClaims() method and specify the signing algorithm using jwt.SigningMethodHS256. Next, we sign the JWT with the secret key using the jwt.Token.SignedString() method.

When verifying the JWT, we use the jwt.Parse() method to parse the JWT and specify a callback function to verify the signature of the JWT. The callback function must return a value of type interface{} that represents the key used to sign the JWT. In the code above we are using a hardcoded key, but usually we store the key in a safe location and set it via an environment variable or configuration file.

6. Conclusion

In this article, we introduce the basic steps to implement RESTful API in Go language. We use the mux package to create a router and map HTTP methods and routing paths to RESTful API handlers. We also cover security measures such as JWT and HTTPS to protect RESTful APIs. Finally, we also highlight the importance of using the transport object pattern to simplify the design and implementation of RESTful APIs.

The above is the detailed content of How is RESTful API implemented in Go language?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

How to solve the problem that custom structure labels in Goland do not take effect? How to solve the problem that custom structure labels in Goland do not take effect? Apr 02, 2025 pm 12:51 PM

Regarding the problem of custom structure tags in Goland When using Goland for Go language development, you often encounter some configuration problems. One of them is...

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

Why do all values ​​become the last element when using for range in Go language to traverse slices and store maps? Why do all values ​​become the last element when using for range in Go language to traverse slices and store maps? Apr 02, 2025 pm 04:09 PM

Why does map iteration in Go cause all values ​​to become the last element? In Go language, when faced with some interview questions, you often encounter maps...

Go language slice: Why does it not report an error when single-element slice index 1 intercept? Go language slice: Why does it not report an error when single-element slice index 1 intercept? Apr 02, 2025 pm 02:24 PM

Go language slice index: Why does a single-element slice intercept from index 1 without an error? In Go language, slices are a flexible data structure that can refer to the bottom...

See all articles