Order By in GoLang and MySQL

This project will demonstrate how to use Min and Max in GoLang and MySQL

  • LearnGoLangAndMySQL
    • src
      • config
        • config.go
      • entities
        • product_entity.go
      • models
        • product_model.go
      • main.go
package models

import (
    "database/sql"
    "entities"
)

type ProductModel struct {
    Db *sql.DB
}

func (productModel ProductModel) OrderByASC() ([]entities.Product, error) {
    rows, err := productModel.Db.Query("select * from product order by price asc")
    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) OrderByDESC() ([]entities.Product, error) {
    rows, err := productModel.Db.Query("select * from product order by price desc")
    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
    }
}
package main

import (
    "config"
    "fmt"
    "models"
)

func main() {

    fmt.Println("Sort by Price Ascending")
    SortPriceAscending()

    fmt.Println("Sort by Price Descending")
    SortPriceDescending()

}

func SortPriceAscending() {
    db, err := config.GetMySQLDB()
    if err != nil {
        fmt.Println(err)
    } else {
        productModel := models.ProductModel{
            Db: db,
        }
        products, err2 := productModel.OrderByASC()
        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 SortPriceDescending() {
    db, err := config.GetMySQLDB()
    if err != nil {
        fmt.Println(err)
    } else {
        productModel := models.ProductModel{
            Db: db,
        }
        products, err2 := productModel.OrderByDESC()
        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("----------------------------")
            }
        }
    }
}
package entities

type Product struct {
    Id       int64
    Name     string
    Price    float32
    Quantity int
    Status   bool
}
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
}

Screenshots