{"id":2403,"date":"2018-08-29T01:49:24","date_gmt":"2018-08-28T16:49:24","guid":{"rendered":"http:\/\/okamurax.com\/?p=2403"},"modified":"2020-05-06T00:18:12","modified_gmt":"2020-05-05T15:18:12","slug":"golang-%e3%83%86%e3%82%b9%e3%83%88%e3%81%a7%e5%b0%8f%e3%81%95%e3%81%84%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2403","title":{"rendered":"golang \u30c6\u30b9\u30c8\u3067\u5c0f\u3055\u3044\u30d7\u30ed\u30b0\u30e9\u30e0"},"content":{"rendered":"<p>golang\u306f\u3058\u3081\u3066\u9593\u3082\u306a\u3044\u306e\u3067\u8abf\u3079\u3064\u3064\u8272\u3005\u8a66\u9a13\u4e2d<\/p>\n<p>\u6a19\u6e96\u5165\u529b\u304b\u3089\u65e5\u4ed8\u5224\u65ad<\/p>\n<pre class=\"lang:go decode:true\">package main\r\n\r\nimport (\r\n\t\"bufio\"\r\n\t\"fmt\"\r\n\t\"os\"\r\n\t\"time\"\r\n)\r\n\r\nfunc main() {\r\n\r\n\ttoday := time.Now().Format(\"2\")\r\n\r\n\tif today == \"29\" {\r\n\t\tfmt.Println(\"hit\")\r\n\t\ts := bufio.NewScanner(os.Stdin)\r\n\t\ts.Scan()\r\n\t}\r\n}\r\n<\/pre>\n<p>SQLite\u63a5\u7d9a<\/p>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport(\r\n\t_ \"os\"\r\n\t\"fmt\"\r\n\t\"database\/sql\"\r\n\r\n\t_ \"github.com\/mattn\/go-sqlite3\"\r\n)\r\n\r\nfunc main(){\r\n\r\n\tfmt.Println(\"start\")\r\n\r\n\tdb, err := sql.Open(\"sqlite3\", \".\/test.d\")\r\n\tif err != nil{\r\n\t\tfmt.Println(err)\r\n\t\treturn\r\n\t}\r\n\r\n\tdefer db.Close()\r\n\tdefer fmt.Println(\"db close\")\r\n\r\n\t_, err = db.Exec(\"create table t1 (id int)\")\r\n\tif err != nil{\r\n\t\tfmt.Println(err)\r\n\t\t\/\/return\r\n\t}\r\n\r\n\t_, err = db.Exec(\"insert into t1 (id) values (2)\")\r\n\tif err != nil{\r\n\t\tfmt.Println(err)\r\n\t\treturn\r\n\t}\r\n\r\n\tvar c int\r\n\trows, err := db.Query(\"select count(*) as c from sqlite_master where name = 't1'\")\r\n\tif err != nil{\r\n\t\tfmt.Println(err)\r\n\t\treturn\r\n\t}\r\n\tdefer rows.Close()\r\n\tdefer fmt.Println(\"rows close\")\r\n\r\n\tfor rows.Next(){\r\n\t\terr := rows.Scan(&amp;c)\r\n\t\tif err != nil{\r\n\t\t\treturn\r\n\t\t}\r\n\t\tfmt.Println(c)\r\n\t}\r\n\tfmt.Println(\"end\")\r\n}\r\n<\/pre>\n<p>\u30b5\u30fc\u30d0\u30fc<\/p>\n<pre class=\"lang:go decode:true \">package main\r\nimport (\r\n\t\"fmt\"\r\n\t\"net\/http\"\r\n)\r\n\r\ntype Str string\r\nfunc (s Str) ServeHTTP(w http.ResponseWriter, r *http.Request){\r\n\tfmt.Fprint(w, s)\r\n}\r\n\r\nfunc main() {\r\n\r\n\tfmt.Println(\"server is running\")\r\n\r\n\t\/* HandleFunc *\/\r\n\thttp.HandleFunc(\"\/\", handler)\r\n\r\n\thttp.HandleFunc(\"\/b\", func(w http.ResponseWriter, r *http.Request){\r\n\t\tfmt.Fprint(w, \"Hello b\")\r\n\t})\r\n\r\n\thttp.HandleFunc(\"\/e\", Rec)\r\n\r\n\t\/* Handle *\/\r\n\thttp.Handle(\"\/c\", Str(\"hello c\"))\/\/ServeHTTP\u3092\u6301\u3063\u305f\u69cb\u9020\u4f53\u3092\u6e21\u305b\u3070OK\r\n\r\n\t\/\/\u30d5\u30a1\u30a4\u30eb\u914d\u4fe1\r\n\thttp.Handle(\"\/d\", http.StripPrefix(\"\/d\", http.FileServer(http.Dir(\"file\"))))\r\n\thttp.ListenAndServe(\":8000\", nil)\r\n}\r\n\r\nfunc Rec(w http.ResponseWriter, r *http.Request){\r\n\tif r.Method == http.MethodGet{\r\n\t\tfmt.Fprint(w, r.URL.RawQuery)\r\n\t}\r\n}\r\n\r\nfunc handler(w http.ResponseWriter, r *http.Request){\r\n\tfmt.Fprint(w, \"Hello a\")\r\n\t\/\/fmt.Fprint(w, &amp;url.URL)\r\n}\r\n<\/pre>\n<p>SSH\u63a5\u7d9a<\/p>\n<pre class=\"lang:go decode:true \">package main\r\nimport(\r\n\t\"io\/ioutil\"\r\n\t\"golang.org\/x\/crypto\/ssh\"\r\n)\r\n\r\nfunc main(){\r\n\tip := \"xxx\"\r\n\tport := \"xxx\"\r\n\tuser := \"xxx\"\r\n\r\nbuf, err := ioutil.ReadFile(\"t.pem\")\r\nif err != nil{\r\n\treturn\r\n}\r\nkey, err := ssh.ParsePrivateKey(buf)\r\nif err != nil{\r\n\treturn\r\n}\r\n\r\nconfig := &amp;ssh.ClientConfig{\r\n\tUser: user,\r\n\tAuth: []ssh.AuthMethod{\r\n\t\tssh.PublicKeys(key),\r\n\t},\r\n\tHostKeyCallback: ssh.InsecureIgnoreHostKey(),\r\n}\r\n\r\nconn, err := ssh.Dial(\"tcp\", ip + \":\" + port, config)\r\nif err != nil{\r\n\treturn\r\n}\r\ndefer conn.Close()\r\n\r\nsession, err := conn.NewSession()\r\nif err != nil{\r\n\treturn\r\n}\r\ndefer session.Close()\r\n\r\nsession.Run(\"echo -n &gt; a.txt\")\r\n\r\n}\r\n<\/pre>\n<p>\u4ed6\u8a00\u8a9e\u3067\u8a00\u3046\u30af\u30e9\u30b9<\/p>\n<pre class=\"lang:go decode:true \">package main\r\n\r\nimport(\r\n  \"fmt\"\r\n)\r\n\r\ntype greeting struct{\r\n  words string\r\n}\r\n\r\nfunc (g *greeting) sayHello(){\r\n  fmt.Println(g.words)\r\n}\r\n\r\nfunc main(){\r\n\r\n  s := &amp;greeting{\r\n    words : \"hello world\",\r\n  }\r\n\r\n  s.sayHello() \/\/hello world\r\n\r\n}<\/pre>\n<p>json\u8aad\u307f\u8fbc\u307f<\/p>\n<pre class=\"lang:go decode:true \">package main\r\n\r\nimport(\r\n  \"fmt\"\r\n  \"io\/ioutil\"\r\n  \"encoding\/json\"\r\n)\r\n\r\ntype Personal struct{\r\n  Name string `json:\"addr\"`\r\n  Age int\r\n}\r\n\r\n\/\/\u3053\u3046\u66f8\u304f\u3068json\u306eaddr\u30ad\u30fc\u304c\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u308b\r\n\r\nfunc main(){\r\n\r\n  b, err := ioutil.ReadFile(\".\/data.json\")\r\n  if err != nil{\r\n    panic(err)\r\n  }\r\n\r\n  \/\/\u76f4\u63a5\u66f8\u304f\u5834\u5408\r\n  \/\/b := []byte(`{ \"name\":\"taro\", \"age\":20 }`)\r\n\r\n  var p Personal\r\n  if err := json.Unmarshal(b, &amp;p); err != nil{\r\n    panic(err)\r\n  }\r\n\r\n  fmt.Println(p.Name)\r\n}<\/pre>\n<p>\u6642\u9593<\/p>\n<pre class=\"lang:go decode:true  \">package main\r\n\r\nimport(\r\n  \"time\"\r\n  \"math\/rand\"\r\n)\r\n\r\nfunc main(){\r\n\r\n  \/\/\u65e5\u4ed8\u53d6\u5f97\r\n  println(time.Now().Format(\"2006-01-02\"))\r\n\r\n  \/\/UNIX\u30bf\u30a4\u30e0\r\n  println(time.Now().Unix())\r\n\r\n  \/\/\u4e71\u6570\r\n  println(rand.Int63())\r\n\r\n  \/\/\u6642\u523bSeed\r\n  rand.Seed(time.Now().UnixNano())\r\n\r\n  for i := 1; i &lt;= 2; i++{\r\n    \/\/63bit\r\n    println(rand.Int63())\r\n    \/\/n\u4ed8\u304d\u306f\u7bc4\u56f2\r\n    println(rand.Intn(10))\r\n  }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>golang\u306f\u3058\u3081\u3066\u9593\u3082\u306a\u3044\u306e\u3067\u8abf\u3079\u3064\u3064\u8272\u3005\u8a66\u9a13\u4e2d \u6a19\u6e96\u5165\u529b\u304b\u3089\u65e5\u4ed8\u5224\u65ad package main import ( &#8220;bufio&#8221; &#8220;fmt&#8221; &#8220;os&#8221; &#8220;time&#8221; ) func main() { today :=  &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2403\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;golang \u30c6\u30b9\u30c8\u3067\u5c0f\u3055\u3044\u30d7\u30ed\u30b0\u30e9\u30e0&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[148],"class_list":["post-2403","post","type-post","status-publish","format-standard","hentry","category-1","tag-golang"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2403"}],"version-history":[{"count":7,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2403\/revisions"}],"predecessor-version":[{"id":2432,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2403\/revisions\/2432"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}