使用 Golang 创建 Redis 数据

使用 Golang 创建 Redis 数据。

程式

// ---------------------------------------------------------------
//
//	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)
		}
}

// ----------------------------------------------------------------

执行命令

go run redis_create.go

确认过的版本

$ go version
go version go1.19.2 linux/amd64
广告
将在 10 秒后关闭
bannerAds