0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Golang で Redis のデータを作成 (Create)

Last updated at Posted at 2018-05-21

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

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

実行コマンド

go run redis_create.go

確認したバージョン

$ go version
go version go1.19.2 linux/amd64
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?