Skip to content

gigilin7/Go-Restful-API-MySQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Go-Restful-API-MySQL

Build a message board using Golang, Gin and MySQL. It can do CRUD for management users.

Database

  • Download MySQL
  • Open MySQL workbench and build MySQL connections
  • Create schema ( In MySQL, schema is equivalent to database )
    • In this project, schema name is go_database ( You can see in sql/connect.yaml )

Structure

.
├── controller
│   └── controller.go
├── model
│   └── model.go
├── repository
│   └── repository.go
├── router
│   └── router.go
└── sql
│   ├── connect.yaml
│   └── sql.go
├── go.mod
├── go.sum
└── main.go

Explain the individual functions and functions of the above folders:

  • controller:Check logic of CRUD operation.
  • model:The data object.
  • repository:Functions of CRUD operation with database.
  • router:Set up website URL routing.
  • sql:The setting of database.

Notice ⚠

  • Following changes in Go 1.16, functionality in ioutil has been moved to the os package. Therefore, ioutil.ReadFile should be changed to os.ReadFile

  • If you encounter problems like: {"message":"Error 1146 (42S02): Table 'go_database.message' doesn't exist"} when you want to query message. Use the AutoMigrate method to automatically create data tables. ( You can see in sql/sql.go )

    err = Connect.AutoMigrate(&model.Message{})
    if err != nil {
      return fmt.Errorf("error running AutoMigrate: %w", err)
    }

Implement

 go run main.go

CRUD operation instructions:

curl http://localhost:8081/api/v1/message
curl http://localhost:8081/api/v1/message/:id
curl -X POST http://localhost:8081/api/v1/message \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'User_Id=1&Content=Hi there'
curl -X PATCH http://localhost:8081/api/v1/message/3 \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'Content=Test three!!!'
curl -X DELETE http://localhost:8081/api/v1/message/:id

Result

Query all messages - first time

Create one message

Query all messages

Update one message

Database in MySQL

Reference for learning

About

Build Rest API with Golang, Gin and MySQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages