add english version
This commit is contained in:
58
en/code/src/apps/ch.5.6/mongodb/main.go
Normal file
58
en/code/src/apps/ch.5.6/mongodb/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Example code for Chapter 5.6 from "Build Web Application with Golang"
|
||||
// Purpose: Shows you have to perform basic CRUD operations for a mongodb driver.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
Phone string
|
||||
}
|
||||
|
||||
func checkError(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
DB_NAME = "test"
|
||||
DB_COLLECTION = "people"
|
||||
)
|
||||
|
||||
func main() {
|
||||
session, err := mgo.Dial("localhost")
|
||||
checkError(err)
|
||||
defer session.Close()
|
||||
|
||||
session.SetMode(mgo.Monotonic, true)
|
||||
|
||||
c := session.DB(DB_NAME).C(DB_COLLECTION)
|
||||
err = c.DropCollection()
|
||||
checkError(err)
|
||||
|
||||
ale := Person{"Ale", "555-5555"}
|
||||
cla := Person{"Cla", "555-1234"}
|
||||
|
||||
fmt.Println("Inserting")
|
||||
err = c.Insert(&ale, &cla)
|
||||
checkError(err)
|
||||
|
||||
fmt.Println("Updating")
|
||||
ale.Phone = "555-0101"
|
||||
err = c.Update(bson.M{"name": "Ale"}, &ale)
|
||||
|
||||
fmt.Println("Querying")
|
||||
result := Person{}
|
||||
err = c.Find(bson.M{"name": "Ale"}).One(&result)
|
||||
checkError(err)
|
||||
fmt.Println("Phone:", result.Phone)
|
||||
|
||||
fmt.Println("Deleting")
|
||||
err = c.Remove(bson.M{"name": "Ale"})
|
||||
checkError(err)
|
||||
}
|
||||
6
en/code/src/apps/ch.5.6/mongodb/readme.md
Normal file
6
en/code/src/apps/ch.5.6/mongodb/readme.md
Normal file
@@ -0,0 +1,6 @@
|
||||
##Setup for `ch.5.6` for MongoDB
|
||||
|
||||
- Step 1) Install and run MongoDB
|
||||
- Step 2) Launch the MongoDB daemon (mongod) to start the server.
|
||||
- Step 3) Run `go get` to download and install the remote packages.
|
||||
- Step 4) Execute the program with `go run main.go`
|
||||
60
en/code/src/apps/ch.5.6/redis/main.go
Normal file
60
en/code/src/apps/ch.5.6/redis/main.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Example code for Chapter 5.6 from "Build Web Application with Golang"
|
||||
// Purpose: Shows you have to perform basic CRUD operations for a redis driver.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/astaxie/goredis"
|
||||
)
|
||||
|
||||
func checkError(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
DB_PORT = "9191"
|
||||
DB_URL = "127.0.0.1"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var client goredis.Client
|
||||
|
||||
// Set the default port in Redis
|
||||
client.Addr = DB_URL + ":" + DB_PORT
|
||||
|
||||
// string manipulation
|
||||
fmt.Println("Inserting")
|
||||
err := client.Set("a", []byte("hello"))
|
||||
checkError(err)
|
||||
|
||||
// list operation
|
||||
vals := []string{"a", "b", "c", "d"}
|
||||
for _, v := range vals {
|
||||
err = client.Rpush("l", []byte(v))
|
||||
checkError(err)
|
||||
}
|
||||
fmt.Println("Updating")
|
||||
err = client.Set("a", []byte("a is for apple"))
|
||||
checkError(err)
|
||||
err = client.Rpush("l", []byte("e"))
|
||||
checkError(err)
|
||||
|
||||
fmt.Println("Querying")
|
||||
val, err := client.Get("a")
|
||||
checkError(err)
|
||||
fmt.Println(string(val))
|
||||
|
||||
dbvals, err := client.Lrange("l", 0, 4)
|
||||
checkError(err)
|
||||
for i, v := range dbvals {
|
||||
println(i, ":", string(v))
|
||||
}
|
||||
|
||||
fmt.Println("Deleting")
|
||||
_, err = client.Del("l")
|
||||
checkError(err)
|
||||
_, err = client.Del("a")
|
||||
checkError(err)
|
||||
}
|
||||
10
en/code/src/apps/ch.5.6/redis/readme.md
Normal file
10
en/code/src/apps/ch.5.6/redis/readme.md
Normal file
@@ -0,0 +1,10 @@
|
||||
##Setup for `ch.5.6` for Redis
|
||||
|
||||
- Step 1) Install and run Redis
|
||||
- Step 2) Launch the Redis server matching the DB constants.
|
||||
|
||||
DB_PORT = "9191"
|
||||
DB_URL = "127.0.0.1"
|
||||
|
||||
- Step 3) Run `go get` to download and install the remote packages.
|
||||
- Step 4) Execute the program with `go run main.go`
|
||||
Reference in New Issue
Block a user