Golang で Redis のデータを読む (Read)

Golang で Redis のデータを読みます。
次のプログラムで作成したデータを読みます。
[Golang で Redis のデータを作成 (Create)](https://qiita.com/ekzemplaro/items/ffd36443f40c61531062)

## プログラム

```golang:redis_read.go
// ----------------------------------------------------------------
//
//	redis_read.go
//
//					May/21/2018
//
// ----------------------------------------------------------------
package main
 
import (
	"fmt"
	"net"
	"strings"
	"strconv"
	"encoding/json"
	"reflect"
	"sort"
)

// ----------------------------------------------------------------
func main () {

	fmt.Println ("*** 開始 ***")

	hostname := "localhost"
	port := "6379"

	conn, err := net.Dial ("tcp", hostname + ":" + port)
	if err != nil {
		fmt.Println(err)
		return
		}

	keys := key_get_proc (conn)

	fmt.Println (len(keys))

	dict_aa := make (map[string](map[string]interface{}))

	for _,value := range keys {
		json_str := mcached_socket_read_proc (conn,value)

		if 0 < len (json_str) {
			var unit_aa map[string]interface{}
			json.Unmarshal ([]byte(json_str), &unit_aa)
			dict_aa[value] = unit_aa
			}
	}

	conn.Close ()

	dict_display_proc (dict_aa)

	fmt.Println ("*** 終了 ***")
}

// ----------------------------------------------------------------
func key_get_proc (conn net.Conn) []string {
	var keys []string

	string_received := string_get_proc (conn)

	if 0 < len (string_received) {
		datax := strings.Split (string_received,"\n")
		for _, pp := range datax {
			if (0 < len (pp)) && (pp[:1] == "t") {
				keys = append(keys, pp)
				}
			}
		}

	return keys
}
 
// ----------------------------------------------------------------
func string_get_proc (conn net.Conn) string{
	str_received := ""

	comm_aa := "keys *" + "\r\n"

	_, err := conn.Write([]byte(comm_aa))
	if err != nil {
		fmt.Println(err)
		return str_received
		}

	buf := make([]byte, 1024)
	nn, err := conn.Read(buf[:])
	if err != nil {
		fmt.Println(err)
		return str_received
		}

	str_received = string(buf[0:nn])

	return str_received
}

// ----------------------------------------------------------------
func socket_read_proc (conn net.Conn,key_in string) string {
	str_received := ""
	_, err := conn.Write([]byte("get " + key_in + "\r\n"))
	if err != nil {
		fmt.Println(err)
		return str_received
		}

	buf := make([]byte, 1024)
	nn, err := conn.Read(buf[:])
	if err != nil {
		fmt.Println(err)
		return str_received
		}

	str_received = string(buf[0:nn])

	return	str_received
}

// ----------------------------------------------------------------
func mcached_socket_read_proc (conn net.Conn,key_in string) string {
	json_str := ""
	str_received := socket_read_proc (conn,key_in)

	lines := strings.Split(str_received,"\n")

	if (! strings.Contains(lines[0],"END")) {
		json_str = lines[1]
		}

	return	json_str
}

// ----------------------------------------------------------------
func display_record_proc (key string, unit_aa map[string]interface{}) {
	fmt.Printf ("%s\t",key)
	fmt.Printf ("%s\t",unit_aa["name"])
	vv := reflect.TypeOf(unit_aa["population"])
//	fmt.Println(v)

	population := 0
	if (vv.Kind() == reflect.Int) {
		population = unit_aa["population"].(int)
		} else {
	pf := unit_aa["population"].(float64)
	population = int(pf)
		}

	str_population := strconv.Itoa(population)
	fmt.Printf ("%s\t",str_population)
	fmt.Printf ("%s\n",unit_aa["date_mod"])
}

// ----------------------------------------------------------------
func dict_display_proc (dict_aa map[string](map[string]interface{})) {
	var keys []string
	for key,_ := range dict_aa {
	keys = append (keys,key)
	}

	sort.Strings (keys)

	for _, key := range keys {
	display_record_proc (key, dict_aa[key])
	}

}

// ----------------------------------------------------------------
```


実行コマンド

```bash
go run redis_read.go
```

## 確認したバージョン

```text
$ go version
go version go1.19.2 linux/amd64
```