Skip to content

Commit

Permalink
go demo
Browse files Browse the repository at this point in the history
  • Loading branch information
liulixiang1988 committed Jul 14, 2014
1 parent 0a7eaca commit 786ee5f
Show file tree
Hide file tree
Showing 21 changed files with 3,714 additions and 0 deletions.
18 changes: 18 additions & 0 deletions array_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
)

func test(x [2]int) {
fmt.Printf("x: %p\n", &x)
x[1] = 100
}

func main() {
var a = [2]int{}
fmt.Printf("a: %p\n", &a)
fmt.Println(a)
test(a)
fmt.Println(a)
}
17 changes: 17 additions & 0 deletions bit_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
)

const (
_ = iota
KB = 1 << (iota * 10)
MB
GB
TB
PB
EB
YB
ZB
)
14 changes: 14 additions & 0 deletions concurrency_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"
)

func main() {
c := make(chan bool)
go func() {
fmt.Println("Go Go Go!!!")
c <- true
}()
<-c
}
26 changes: 26 additions & 0 deletions controlflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
)

func main() {
if a := 1; a > 1 {
fmt.Println("Hello")
} else { //else必须在}后面
fmt.Println(a)
}

s := "abc"
for i, n := 0, len(s); i < n; i++ { // 常⻅的for 循环,⽀持初始化语句。
fmt.Println(s[i])
}
n := len(s)
for n > 0 { // 替代while (n > 0) {}
println(s[n-1]) // 替代for (; n > 0;) {}
n--
}
for false { // 替代while (true) {}
println(s) // 替代for (;;) {}
}
}
55 changes: 55 additions & 0 deletions db/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package db

import (
"fmt"
"log"
"time"

"github.com/go-xorm/xorm"
_ "github.com/lib/pq"
_ "github.com/lunny/godbc"
)

//明细表
type Tb_weigh_datalineinfo_detail struct {
Id int64
BatchNumber int64 `xorm:"BatchNumber"`
CarNumber string `xorm:"varchar(30) CarNumber"`
Weight float64 `xorm:'Weight'`
WeighTime time.Time `xorm:"DateTime WeighTime"`
OperateBit int64 `xorm:"OperateBit"`
Remark string `xorm: "Remark"`
Attribute1 string `xorm:"varchar(100) Attribute1"`
Attribute2 string `xorm:"varchar(100) Attribute2"`
Attribute3 string `xorm:"varchar(100) Attribute3"`
Attribute4 string `xorm:"varchar(100) Attribute4"`
Attribute5 string `xorm:"varchar(100) Attribute5"`
}

var mssql *xorm.Engine
var pqorm *xorm.Engine

func NewDetail(batchNumber int64, weight float64) error {
_, err := mssql.Insert(&Tb_weigh_datalineinfo_detail{BatchNumber: batchNumber, Weight: weight, WeighTime: time.Now(), OperateBit: 1})
return err
}

func init() {
fmt.Println("main init")
//创建mssql orm引擎
var err error
mssql, err = xorm.NewEngine("odbc", "driver={sql server};server=127.0.0.1;port=1433;uid=sa;pwd=tlys.oaxmz.5860247;database=tgzljl_czgl")
if err != nil {
log.Fatalf("err happened", err)
}

//创建pq orm引擎
pqorm, err = xorm.NewEngine("postgres", "host=127.0.0.1 port=5432 user=zljl password=123 dbname=tgzljl_czgl sslmode=disable")
if err != nil {
log.Fatalln("pq orm created error:", err)
}
err = pqorm.Sync(new(Tb_weigh_datalineinfo_detail))
if err != nil {
log.Fatalln("pq orm sync error:", err)
}
}
55 changes: 55 additions & 0 deletions func_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
)

func A(a, b, c int) (r int, e string) {
e = "hello"
r = a + b + c
return
}

func S(a ...int) (r int) {
fmt.Println(r)
for _, b := range a {
r += b
}
return
}

func closure(x int) func(int) int {
return func(y int) int {
return x + y
}
}

func panic_demo() {
defer func() {
if err := recover(); err != nil {
fmt.Println("recover demo")
}
}()
panic("panic demo")
}

func main() {
f, s := A(1, 2, 3)
fmt.Println(f, s)
a := S(1, 2, 3, 4, 5)
fmt.Println(a)

b := closure(1)
fmt.Println(b(2))

defer fmt.Println(a)
defer fmt.Println(b(2))

for i := 0; i < 3; i++ {
defer func() {
fmt.Println(i)
}()
}

panic_demo()
}
34 changes: 34 additions & 0 deletions go_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"time"
)

func main() {
c1 := make(chan string)
go func() {
c1 <- "hello"
fmt.Println("write \"hello\" done!")

c1 <- "world"
fmt.Println("write \"world\" done!")

fmt.Println("write go to sleep")
time.Sleep(3 * time.Second)
c1 <- "channel"
fmt.Println("write \"channel\" done!")
}()

time.Sleep(2 * time.Second)
fmt.Println("Read wake up")

msg := <-c1
fmt.Println("Reader ", msg)

msg = <-c1
fmt.Println("Reader ", msg)

msg = <-c1
fmt.Println("Reader ", msg)
}
7 changes: 7 additions & 0 deletions hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello, world! 你好,世界!")
}
14 changes: 14 additions & 0 deletions int_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"
"strconv"
)

func main() {
var a = 65
b := strconv.Itoa(a)
fmt.Println(b)
a, _ = strconv.Atoi(b) #返回多个值注意
fmt.Println(a)
}
30 changes: 30 additions & 0 deletions interface_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
)

type empty interface {
}

type PhoneConnector struct {
name string
}

func main() {
phoneConnector := PhoneConnector{name: "Phone Connector"}
Disconnect(phoneConnector)
}

func Disconnect(usb empty) {
if phoneConnector, ok := usb.(PhoneConnector); ok {
fmt.Println("Phone disconnect:", phoneConnector.name)
}

switch v := usb.(type) {
case PhoneConnector:
fmt.Println("Phone disconnect:", v.name)
default:
fmt.Println("Unknow device")
}
}
12 changes: 12 additions & 0 deletions ipinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/yinheli/qqwry"
"log"
)

func main() {
q := qqwry.NewQQwry("qqwry.dat")
q.Find("180.89.94.90")
log.Printf("ip:%v, 国家:%v, City:%v", q.IP, q.Country, q.City)
}
21 changes: 21 additions & 0 deletions map_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
)

func main() {
a := map[string]struct {
name string
age int
}{
"lixiang": {"理想", 26},
"longlong": {"龙珑", 26},
}
fmt.Println(a)
var m map[string]string = make(map[string]string)
m["lixiang"] = "lixiang"
fmt.Println(m)
delete(m, "lixiang")
fmt.Println(m)
}
28 changes: 28 additions & 0 deletions method_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
)

type Person struct {
name string
age int
}

func (per *Person) Profile() {
fmt.Println(per.name, per.age)
}

type INT int

func (i *INT) Increment() {
*i += INT(100)
}

func main() {
p := Person{name: "lixiang", age: 27}
p.Profile()
var a INT = 2
a.Increment()
fmt.Println(a)
}
14 changes: 14 additions & 0 deletions panic_recover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func test() {
defer func() {
if err := recover(); err != nil {
println(err.(string))
}
}()
panic("panic error")
}

func main() {
test()
}
Loading

0 comments on commit 786ee5f

Please sign in to comment.