golangはじめて間もないので調べつつ色々試験中
標準入力から日付判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package main import ( "bufio" "fmt" "os" "time" ) func main() { today := time.Now().Format("2") if today == "29" { fmt.Println("hit") s := bufio.NewScanner(os.Stdin) s.Scan() } } |
SQLite接続
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package main import( _ "os" "fmt" "database/sql" _ "github.com/mattn/go-sqlite3" ) func main(){ fmt.Println("start") db, err := sql.Open("sqlite3", "./test.d") if err != nil{ fmt.Println(err) return } defer db.Close() defer fmt.Println("db close") _, err = db.Exec("create table t1 (id int)") if err != nil{ fmt.Println(err) //return } _, err = db.Exec("insert into t1 (id) values (2)") if err != nil{ fmt.Println(err) return } var c int rows, err := db.Query("select count(*) as c from sqlite_master where name = 't1'") if err != nil{ fmt.Println(err) return } defer rows.Close() defer fmt.Println("rows close") for rows.Next(){ err := rows.Scan(&c) if err != nil{ return } fmt.Println(c) } fmt.Println("end") } |
サーバー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package main import ( "fmt" "net/http" ) type Str string func (s Str) ServeHTTP(w http.ResponseWriter, r *http.Request){ fmt.Fprint(w, s) } func main() { fmt.Println("server is running") /* HandleFunc */ http.HandleFunc("/", handler) http.HandleFunc("/b", func(w http.ResponseWriter, r *http.Request){ fmt.Fprint(w, "Hello b") }) http.HandleFunc("/e", Rec) /* Handle */ http.Handle("/c", Str("hello c"))//ServeHTTPを持った構造体を渡せばOK //ファイル配信 http.Handle("/d", http.StripPrefix("/d", http.FileServer(http.Dir("file")))) http.ListenAndServe(":8000", nil) } func Rec(w http.ResponseWriter, r *http.Request){ if r.Method == http.MethodGet{ fmt.Fprint(w, r.URL.RawQuery) } } func handler(w http.ResponseWriter, r *http.Request){ fmt.Fprint(w, "Hello a") //fmt.Fprint(w, &url.URL) } |
SSH接続
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package main import( "io/ioutil" "golang.org/x/crypto/ssh" ) func main(){ ip := "xxx" port := "xxx" user := "xxx" buf, err := ioutil.ReadFile("t.pem") if err != nil{ return } key, err := ssh.ParsePrivateKey(buf) if err != nil{ return } config := &ssh.ClientConfig{ User: user, Auth: []ssh.AuthMethod{ ssh.PublicKeys(key), }, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } conn, err := ssh.Dial("tcp", ip + ":" + port, config) if err != nil{ return } defer conn.Close() session, err := conn.NewSession() if err != nil{ return } defer session.Close() session.Run("echo -n > a.txt") } |
他言語で言うクラス
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package main import( "fmt" ) type greeting struct{ words string } func (g *greeting) sayHello(){ fmt.Println(g.words) } func main(){ s := &greeting{ words : "hello world", } s.sayHello() //hello world } |
json読み込み
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package main import( "fmt" "io/ioutil" "encoding/json" ) type Personal struct{ Name string `json:"addr"` Age int } //こう書くとjsonのaddrキーがマッピングされる func main(){ b, err := ioutil.ReadFile("./data.json") if err != nil{ panic(err) } //直接書く場合 //b := []byte(`{ "name":"taro", "age":20 }`) var p Personal if err := json.Unmarshal(b, &p); err != nil{ panic(err) } fmt.Println(p.Name) } |
時間
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package main import( "time" "math/rand" ) func main(){ //日付取得 println(time.Now().Format("2006-01-02")) //UNIXタイム println(time.Now().Unix()) //乱数 println(rand.Int63()) //時刻Seed rand.Seed(time.Now().UnixNano()) for i := 1; i <= 2; i++{ //63bit println(rand.Int63()) //n付きは範囲 println(rand.Intn(10)) } } |