Skip to content

Instantly share code, notes, and snippets.

View muhammadisa's full-sized avatar
🚀
Keep grow

Muhammad Isa muhammadisa

🚀
Keep grow
View GitHub Profile
package main
import (
"fmt"
"log"
"net/http"
)
// data variable
var messageChan chan string
package repository
type Repository interface {
ReadExamples() ([]Example, error)
ReadExampleByID(id int64) (Example, error)
WriteExample(example Example) error
UpdateExample(example Example) error
RemoveExampleByID(id int64) error
}
package repository
type Example struct {
ID int64
Content string
}
func (e *Example) FastScan() []interface{} {
return []interface{}{
&e.ID,
package repository
import (
"database/sql"
)
type repo struct {
db *sql.DB
}
package repository
func (r repository) ReadExampleByID(id int64) (Example, error) {
const query = `SELECT * FROM example WHERE id = ?`
var example Example
stmt, err := r.db.Prepare(query)
if err != nil {
return Example{}, err
}
package repository
func (r repo) ReadExampleByID(id int64) (Example, error) {
const query = `SELECT * FROM example WHERE id = ?`
var example Example
stmt, err := r.db.Prepare(query)
if err != nil {
return Example{}, err
}
package repository
func (r repo) ReadExamples() ([]Example, error) {
const query = `SELECT * FROM example`
var examples []Example
stmt, err := r.db.Prepare(query)
if err != nil {
return nil, err
}
package repository
func (r repo) RemoveExampleByID(id int64) error {
const query = `DELETE FROM example WHERE id = ?`
stmt, err := r.db.Prepare(query)
if err != nil {
return err
}
package repository
func (r repo) UpdateExample(example Example) error {
const query = `UPDATE example SET content = ? WHERE id = ?`
stmt, err := r.db.Prepare(query)
if err != nil {
return err
}
package repository
func (r repo) WriteExample(example Example) error {
const query = `INSERT INTO example(content) VALUES (?)`
stmt, err := r.db.Prepare(query)
if err != nil {
return err
}