Skip to content

Instantly share code, notes, and snippets.

@svanellewee
Created August 19, 2016 12:45
Show Gist options
  • Save svanellewee/b469760bb162688401039fbe2f383015 to your computer and use it in GitHub Desktop.
Save svanellewee/b469760bb162688401039fbe2f383015 to your computer and use it in GitHub Desktop.
GO kitchen sink!
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/golang/protobuf/proto"
)
var (
name, location string
age int
)
func TryDatabase() {
db, err := sql.Open("mysql", "root:@/pricing_service_test")
if err != nil {
panic(fmt.Sprintf("BLA %s", err))
}
defer db.Close()
rows, err2 := db.Query("SELECT pricing_model_id, pricing_model_name FROM pricing_models")
if err2 != nil {
panic(fmt.Sprintf("BLA2 %s", err2))
}
defer rows.Close()
for rows.Next() {
var id int
var name string
_ = rows.Scan(&id, &name)
fmt.Printf("-->>> %d %s\n", id, name)
}
}
func testfunc(message string) func() {
x := fmt.Sprintf("Some message I've enclosed [%s]", message)
return func() {
fmt.Println("and now for something completely different [%s]\n", x)
}
}
func add(args ...int) int {
total := 0
for _, v := range args {
total += v
}
return total
}
type IntFunc func(...int) int
type IntBinaryFunc func(int, int) int
// func doIntFunc(fn func(x, y int) int, x, y int) int {
// return fn(x, y)
// }
type Product struct {
idProduct int
Stock int
CostPrice float32
}
func (p Product) String() string {
return fmt.Sprintf("Product:{id=%d..%d..R %f}",
p.idProduct,
p.Stock,
p.CostPrice)
}
func doIntFunc(fn IntFunc, x, y int) int {
return fn(x, y)
}
func doIntFunc2(fn IntFunc, x, y, z int) int {
return fn(x, y, z)
}
func main() {
name = "Stephan"
age = 100
resultfunc := testfunc("BLA YADDA")
name, location := "Bob", "here, there, everywhere"
fmt.Printf("Horses, of courses %s.. %d.. [%s]\n", name, age, location)
resultfunc()
fmt.Printf("How is this? fn(%d, %d) = %d\n", 10, 12, doIntFunc(add, 10, 12))
newProduct := Product{idProduct: 10, Stock: 10, CostPrice: 100.0}
fmt.Printf("Hello %v\n", newProduct)
lookup := map[string]int{
"hello": 10,
"bye": 100,
}
for key, value := range lookup {
fmt.Printf("%s... %d\n", key, value)
}
TryDatabase()
test := &Test{
Label: proto.String("hello"),
Type: proto.Int32(17),
Optionalgroup: &Test_OptionalGroup{
RequiredField: proto.String("good bye"),
},
}
data, err := proto.Marshal(test)
if err != nil {
fmt.Printf("marshaling error: %s\n", err)
}
newTest := &Test{}
err = proto.Unmarshal(data, newTest)
if err != nil {
fmt.Printf("unmarshaling error: %s\n", err)
}
fmt.Printf("DONE! %v\n", newTest)
}
// http://tleyden.github.io/blog/2014/12/02/getting-started-with-go-and-protocol-buffers/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment