Golang p1:Setup gin,mysql

Setup project:

mkdir gin
cd gin
go mod init demo

Setup gin

go get -u github.com/gin-gonic/gin

Setup mysql

go get -u github.com/go-sql-driver/mysql

Setup database with name gin

USE gin;
DROP TABLE IF EXISTS cities;
CREATE TABLE cities(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), population INT);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);

Create file main.go
Chú ý line 18

 db, err := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/database")

Trong đó
user:user mysql
pass:passmysql
database:name database

Run

go run main.go

Test gin connect: http://localhost:8080/ping
Test connect mysql:http://localhost:8080/test