Install Libraries
Make sure Git is installed on your machine and in your system’s PATH. Install the package to your $GOPATH with the go tool from shell:
$ go get github.com/go-sql-driver/mysql
Create Database
Create a database with the name is learngodb. This database have 1 tables: Product table.
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
`price` double NOT NULL,
`quantity` int(11) NOT NULL,
`status` tinyint(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`id`, `name`, `price`, `quantity`, `status`) VALUES
(1, 'Tivi 1', 200, 4, 1),
(2, 'Tivi 2', 500, 6, 0),
(3, 'Laptop 1', 250, 11, 1),
(4, 'Laptop 2', 241, 6, 0),
(5, 'Laptop 3', 600, 11, 0),
(6, 'Mobile 1', 120, 20, 1),
(7, 'Mobile 2', 210, 25, 0);
Structure of Product Table
Data of Product Table
Create Entity
Create new folder named src. In src folder, create new folder named entities. In this folder, create new file named product_entity.go as below:
package entities
type Product struct {
Id int64
Name string
Price float32
Quantity int
Status bool
}
Create Config
In src folder, create new folder named config. In this folder, create new file named config.go, this file is used to connect mysql database:
package config
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func GetMySQLDB() (db *sql.DB, err error) {
dbDriver := "mysql"
dbUser := "root"
dbPass := "123456"
dbName := "learngodb"
db, err = sql.Open(dbDriver, dbUser + ":" + dbPass + "@/" + dbName)
return
}
Create Model
In src folder, create new folder named models. In models folder, create new file named product_model.go. This file contains methods to interact with the database.
package models
import (
"database/sql"
"entities"
)
type ProductModel struct {
Db *sql.DB
}
func (productModel ProductModel) StartsWith(keyword string) ([]entities.Product, error) {
rows, err := productModel.Db.Query("select * from product where name like ?", keyword + "%")
if err != nil {
return nil, err
} else {
products := []entities.Product{}
for rows.Next() {
var id int64
var name string
var price float32
var quantity int
var status bool
err2 := rows.Scan(&id, &name, &price, &quantity, &status)
if err2 != nil {
return nil, err2
} else {
product := entities.Product{id, name, price, quantity, status}
products = append(products, product)
}
}
return products, nil
}
}
func (productModel ProductModel) EndsWith(keyword string) ([]entities.Product, error) {
rows, err := productModel.Db.Query("select * from product where name like ?", "%" + keyword)
if err != nil {
return nil, err
} else {
products := []entities.Product{}
for rows.Next() {
var id int64
var name string
var price float32
var quantity int
var status bool
err2 := rows.Scan(&id, &name, &price, &quantity, &status)
if err2 != nil {
return nil, err2
} else {
product := entities.Product{id, name, price, quantity, status}
products = append(products, product)
}
}
return products, nil
}
}
func (productModel ProductModel) Contains(keyword string) ([]entities.Product, error) {
rows, err := productModel.Db.Query("select * from product where name like ?", "%" + keyword + "%")
if err != nil {
return nil, err
} else {
products := []entities.Product{}
for rows.Next() {
var id int64
var name string
var price float32
var quantity int
var status bool
err2 := rows.Scan(&id, &name, &price, &quantity, &status)
if err2 != nil {
return nil, err2
} else {
product := entities.Product{id, name, price, quantity, status}
products = append(products, product)
}
}
return products, nil
}
}
Structure of Project
Run Application
In src folder, create new file named main.go as below:
package main
import (
"config"
"fmt"
"models"
)
func main() {
fmt.Println("Start with ti string")
CallStartWithMethod()
fmt.Println("End with top 1 string")
CallEndsWithMethod()
fmt.Println("Contains vi string")
CallContainsMethod()
}
func CallStartWithMethod() {
db, err := config.GetMySQLDB()
if err != nil {
fmt.Println(err)
} else {
productModel := models.ProductModel{
Db: db,
}
products, err2 := productModel.StartsWith("ti")
if err2 != nil {
fmt.Println(err2)
} else {
for _, product := range products {
fmt.Println("Id:", product.Id)
fmt.Println("Name:", product.Name)
fmt.Println("Price:", product.Price)
fmt.Println("Quantity:", product.Quantity)
fmt.Println("Status:", product.Status)
fmt.Println("----------------------------")
}
}
}
}
func CallEndsWithMethod() {
db, err := config.GetMySQLDB()
if err != nil {
fmt.Println(err)
} else {
productModel := models.ProductModel{
Db: db,
}
products, err2 := productModel.EndsWith("top 1")
if err2 != nil {
fmt.Println(err2)
} else {
for _, product := range products {
fmt.Println("Id:", product.Id)
fmt.Println("Name:", product.Name)
fmt.Println("Price:", product.Price)
fmt.Println("Quantity:", product.Quantity)
fmt.Println("Status:", product.Status)
fmt.Println("----------------------------")
}
}
}
}
func CallContainsMethod() {
db, err := config.GetMySQLDB()
if err != nil {
fmt.Println(err)
} else {
productModel := models.ProductModel{
Db: db,
}
products, err2 := productModel.Contains("vi")
if err2 != nil {
fmt.Println(err2)
} else {
for _, product := range products {
fmt.Println("Id:", product.Id)
fmt.Println("Name:", product.Name)
fmt.Println("Price:", product.Price)
fmt.Println("Quantity:", product.Quantity)
fmt.Println("Status:", product.Status)
fmt.Println("----------------------------")
}
}
}
}
Output
Start with ti string
Id: 1
Name: Tivi 1
Price: 200
Quantity: 4
Status: true
----------------------------
Id: 2
Name: Tivi 2
Price: 500
Quantity: 6
Status: false
----------------------------
End with top 1 string
Id: 3
Name: Laptop 1
Price: 250
Quantity: 11
Status: true
----------------------------
Contains vi string
Id: 1
Name: Tivi 1
Price: 200
Quantity: 4
Status: true
----------------------------
Id: 2
Name: Tivi 2
Price: 500
Quantity: 6
Status: false
----------------------------