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 3 years have passed since last update.

Golang で CouchDB のデータを更新 (Update)

Last updated at Posted at 2018-05-20

Golang で CouchDB のデータを更新する方法です。
次のプログラムで作成したデータを更新します。
Golang で CouchDB を使う (Create)

couch_update.go
// ----------------------------------------------------------------
//
//	couch_update.go
//
//					May/20/2018
//
// ----------------------------------------------------------------
package main

import (
	"fmt"
	"encoding/json"
	"os"
	"strconv"
	"strings"
	"net/http"
	"log"
	"io"
	"io/ioutil"
	"time"
)

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

	fmt.Printf ("*** 開始 ***\n")

	key_in := os.Args[1]
	population_in,_ := strconv.Atoi (os.Args[2])

	fmt.Printf ("key_in = %d\t" , key_in)
	fmt.Printf ("population_in = %d\n" , population_in)


	url := "http://localhost:5984/nagano/" + key_in

	json_str := url_get_proc (url)
	fmt.Printf (json_str + "\n")

	var unit_aa map[string]interface{}
	if err := json.Unmarshal([]byte(json_str), &unit_aa); err != nil {
        	panic(err)
		}

	name := unit_aa["name"].(string)
	population := unit_aa["population"].(float64)
	date_mod := unit_aa["date_mod"].(string)
	fmt.Printf ("%s\t%s\t%f\t%s\n",key_in,name,population,date_mod)

	unit_out := make(map[string]interface{})
	unit_out["_rev"] = unit_aa["_rev"].(string)
	unit_out["name"] = name
	unit_out["population"] = population_in
	today := get_current_date_proc ()
	unit_out["date_mod"] = today
	output, _ := json.Marshal(unit_out)
	json_str = string(output)

	fmt.Printf (json_str + "\n")

	putRequest(url, strings.NewReader(json_str))

	fmt.Printf ("*** 終了 ***\n")
}

// ----------------------------------------------------------------
func url_get_proc (url string) string {
	resp, err := http.Get (url)

	json_str := ""

	if err != nil {
		log.Fatalln(err)
		fmt.Printf("%s", err)
		os.Exit(1) 
	} else {
		defer resp.Body.Close()
		 contents, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			fmt.Printf("%s", err)
			os.Exit(1)
			}
		json_str := string(contents)
//		fmt.Printf("%s\n", json_str)
	return (json_str)

		}

	return (json_str)
}

// ----------------------------------------------------------------
func putRequest(url string, data io.Reader)  {
	client := &http.Client{}
	req, err := http.NewRequest(http.MethodPut, url, data)
	if err != nil {
		// handle error
		log.Fatal(err)
	}
	_, err = client.Do(req)
	if err != nil {
		// handle error
		log.Fatal(err)
	}


}

// ----------------------------------------------------------------
func get_current_date_proc () string {
	now := time.Now ()
	fmt.Printf ("%s\t" ,now)
	fmt.Printf ("%d-%d-%d\n" ,now.Year (),now.Month(),now.Day())
	today := strconv.Itoa (now.Year()) + "-" +
		fmt.Sprintf ("%d",now.Month()) + "-" +
		strconv.Itoa (now.Day())

	return	today
}

// ----------------------------------------------------------------
// ----------------------------------------------------------------
Makefile
couch_update: couch_update.go
	go build couch_update.go
clean:
	rm -f couch_update

実行コマンドの例

./couch_update t2032 97321473

次のバージョンで確認しました。

$ go version
go version go1.13.8 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?