Use `gofmt` compliant formatting (#8192)

pull/8245/head
Piotr Idzik 3 days ago committed by GitHub
parent ed8c2f3168
commit 32690e98da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 39
      src/data/guides/golang-rest-api.md

@ -140,13 +140,13 @@ To create a new book handler function, create an `api/handlers.go` file that wil
package api
import (
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"net/http"
"os"
)
var DB *gorm.DB
@ -248,7 +248,7 @@ func DeleteBook(c *gin.Context) {
return
}
ResponseJSON(c, http.StatusOK, "Book deleted successfully", nil)
}
}
```
### Putting it all together
@ -259,8 +259,8 @@ With the handlers set up, you need to create the application entry point and spe
package main
import (
"go_book_api/api"
"github.com/gin-gonic/gin"
"go_book_api/api"
)
func main() {
@ -368,14 +368,14 @@ package tests
import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"go_book_api/api"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func setupTestDB() {
@ -605,10 +605,10 @@ package api
import (
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"net/http"
"os"
)
// Secret key for signing JWT
@ -648,12 +648,12 @@ Next, update the `handlers.go` file with a `GenerateJWT` function that generates
```go
package api
import (
import (
//other imports
"github.com/golang-jwt/jwt/v5"
)
)
func GenerateJWT(c *gin.Context) {
func GenerateJWT(c *gin.Context) {
var loginRequest LoginRequest
if err := c.ShouldBindJSON(&loginRequest); err != nil {
ResponseJSON(c, http.StatusBadRequest, "Invalid request payload", nil)
@ -674,7 +674,7 @@ package api
return
}
ResponseJSON(c, http.StatusOK, "Token generated successfully", gin.H{"token": tokenString})
}
}
```
> In a real-world application, the credentials used to generate the token will be specific to users and not the default ones.
@ -685,8 +685,8 @@ Lastly, update the `main.go` file inside the `cmd` folder to add a public route
package main
import (
"go_book_api/api"
"github.com/gin-gonic/gin"
"go_book_api/api"
)
func main() {
@ -872,16 +872,17 @@ It includes functionality for creating, reading, updating, and deleting books, a
package api
import (
"log"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"net/http"
"os"
"time"
)
var DB *gorm.DB
// InitDB initializes the database connection using environment variables.

Loading…
Cancel
Save