Golang で Redis のデータを作成 (Create)
Golang で Redis のデータを作成します。
redis_create.go
// ---------------------------------------------------------------
//
// redis_create.go
//
// May/21/2018
// ---------------------------------------------------------------
package main
import (
"fmt"
"net"
"encoding/json"
)
// ---------------------------------------------------------------
func data_prepare_proc () map[string](map[string]interface{}) {
dict_aa := make (map[string](map[string]interface{}))
dict_aa["t1851"] = unit_gen_proc ("福井",63152,"1921-10-12")
dict_aa["t1852"] = unit_gen_proc ("敦賀",28671,"1921-9-25")
dict_aa["t1853"] = unit_gen_proc ("小浜",81597,"1921-6-14")
dict_aa["t1854"] = unit_gen_proc ("大野",42178,"1921-4-31")
dict_aa["t1855"] = unit_gen_proc ("勝山",17859,"1921-6-24")
dict_aa["t1856"] = unit_gen_proc ("鯖江",28592,"1921-1-10")
dict_aa["t1857"] = unit_gen_proc ("あわら",42917,"1921-10-19")
dict_aa["t1858"] = unit_gen_proc ("越前",76231,"1921-12-28")
dict_aa["t1859"] = unit_gen_proc ("坂井",21978,"1921-5-14")
return (dict_aa)
}
// ---------------------------------------------------------------
func main () {
fmt.Printf ("*** 開始 ***\n")
dict_aa := data_prepare_proc ()
hostname := "localhost"
port := "6379"
conn, err := net.Dial ("tcp",hostname + ":" + port)
if err != nil {
fmt.Println(err)
return
}
dict_to_redis_proc (conn,dict_aa)
conn.Close ()
fmt.Printf ("*** 終了 ***\n")
}
// ---------------------------------------------------------------
func unit_gen_proc (name string,population int,date_mod string) map[string]interface{} {
unit_aa := make (map[string]interface{})
unit_aa["name"] = name
unit_aa["population"] = population
unit_aa["date_mod"] = date_mod
return (unit_aa)
}
// ----------------------------------------------------------------
func redis_socket_write_proc (conn net.Conn,key_in string,json_str string) {
fmt.Println (key_in)
fmt.Println (json_str)
comm_aa := "set " + key_in + " '" + json_str + "'\r\n"
conn.Write([]byte(comm_aa))
buf := make ([]byte,1024)
conn.Read (buf[:])
fmt.Println (string(buf[0:10]))
}
// ----------------------------------------------------------------
func dict_to_redis_proc (conn net.Conn,dict_aa map[string](map[string]interface{})) {
for key,value := range dict_aa {
// fmt.Print (key + "\t")
// fmt.Println (value["name"])
output, _ := json.Marshal (value)
json_str := string(output)
redis_socket_write_proc (conn,key,json_str)
}
}
// ----------------------------------------------------------------
Makefile
redis_create: redis_create.go
go build redis_create.go
clean:
rm -f redis_create
実行コマンド
./redis_create
次のバージョンで確認しました。
$ go version
go version go1.14.2 linux/amd64
Author And Source
この問題について(Golang で Redis のデータを作成 (Create)), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/ffd36443f40c61531062著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .