更新が滞ってしまっていたので、勉強でやってたついでで投げるスタイル
要は忘備録
わからなかったものは Qiita の誰かの投稿を参考…どころか、そのままだったりする
完成されたコードをこれ以上手直しする必要がないので仕方ないね
目次
A Tour of GO とは
Go言語の公式の入門用、ブラウザでプログラムを動かして試せるサイト
Go言語の公式サイト(https://golang.org/)のDocumentsにリンクがあるのだが、これは英語版
英語のA Tour of Go:(https://tour.golang.org/welcome/1)
これの日本語版が別にある
日本語のA Tour of Go:(https://go-tour-jp.appspot.com/welcome/1)
ありがたみ
ついで
なんかブラウザ上で試せる楽しいサイトも有る
しかもコードを書いてシェアでリンクを作成すると公開できる
勉強が楽かよ
公式っぽい「The Go Playground」:(https://play.golang.org/)
どなたかの「The Go Play Space」:(https://goplay.space/)
詳しくは比べてないのでどちらが好みかは試してください
例題たち
Exerciseと称して「こういうのを用意したからホニャララを自分で作ってみろ(答えはない)」という試練を与えるページがTourの途中に時々ある
ネットで探すと先達たちが回答例を公開している
難しかったら有り難みを感じながら答えを見てから考えよう(諦め
わからないものはわからない
method編の5個をとりあえず書いてみる
先に本来の問題ページ
・https://go-tour-jp.appspot.com/methods/18
・https://go-tour-jp.appspot.com/methods/20
・https://go-tour-jp.appspot.com/methods/22
・https://go-tour-jp.appspot.com/methods/23
・https://go-tour-jp.appspot.com/methods/25
methods 18
・https://go-tour-jp.appspot.com/methods/18
package main import "fmt" type IPAddr [4]byte // TODO: Add a "String() string" method to IPAddr. func (p IPAddr) String() string { return fmt.Sprintf("%v.%v.%v.%v", p[0],p[1],p[2],p[3]) } func main() { hosts := map[string]IPAddr{ "loopback": {127, 0, 0, 1}, "googleDNS": {8, 8, 8, 8}, } for name, ip := range hosts { fmt.Printf("%v: %v\n", name, ip.String()) } }
https://play.golang.org/p/Q4bcrdJFgx6
methods 20
・https://go-tour-jp.appspot.com/methods/20
平方根を出す
Squrt(*)の*に入れる数字を変えて平方
package main import ( "fmt" "math" "errors" ) type ErrNegativeSqrt float64 func (e ErrNegativeSqrt) Error() string{ return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e)) } func Sqrt(x float64) (float64, error) { if x < 0 { e := errors.New(ErrNegativeSqrt(x).Error()) return math.Sqrt(x), e } z := float64(1) for i := 0; i<10; i++{ z = z - (z*z-x)/(2*z) } fmt.Println(z) return math.Sqrt(x), nil } func main() { fmt.Println(Sqrt(2)) fmt.Println(Sqrt(-2)) }
https://play.golang.org/p/Ni2Jqn8TJml
methods 22
・https://go-tour-jp.appspot.com/methods/22
reader.Validate() が何やってるかは、ここを見る
https://github.com/golang/tour/blob/master/reader/validate.go#L13
package main import "golang.org/x/tour/reader" type MyReader struct{} // TODO: Add a Read([]byte) (int, error) method to MyReader. func (v MyReader)Read(b []byte)(int, error){ for i := range b { b[i] = 'A' } return len(b), nil } func main() { reader.Validate(MyReader{}) }
https://play.golang.org/p/7WR-z5mS8IV
methods 23
・https://go-tour-jp.appspot.com/methods/23
問題文にあるROT13が何だっていうパンピーだった
優しいのでリンク付き
package main import ( "io" "os" "strings" ) type rot13Reader struct { r io.Reader } func (rot13 rot13Reader) Read(b []byte) (int, error) { n, err := rot13.r.Read(b) for i := range b { if b[i] >= 'A' && b[i] <= 'M' || b[i] >= 'a' && b[i] <= 'm' { b[i] += 13 } else if b[i] >= 'N' && b[i] <= 'Z' || b[i] >= 'n' && b[i] <= 'z' { b[i] -= 13 } } return n, err } func main() { s := strings.NewReader("Lbh penpxrq gur pbqr!") r := rot13Reader{s} io.Copy(os.Stdout, &r) }
https://play.golang.org/p/22nCBLmNVNv
methods 25
・https://go-tour-jp.appspot.com/methods/25
v := uint8((x+y)/2) の
unit8( ) 内の式を書き換えると生成される画像が変わるので遊べる
package main import ( "golang.org/x/tour/pic" "image" "image/color" ) type Image struct{} func (img Image) ColorModel() color.Model { return color.RGBAModel } func (img Image) Bounds() image.Rectangle { return image.Rect(0, 0, 256, 256) } func (img Image) At(x, y int) color.Color { v := uint8((x+y)/2) return color.RGBA{v, v, 255, 255} } func main() { m := Image{} pic.ShowImage(m) }
https://play.golang.org/p/xZWoR2-OqD2
おわり
ソースコードを書くと文字数がいっぱい
折角だからThe Go Playground を使ったけど、ぶっちゃけソースコードをTourでコピペすれば動くはずだからいらない気がする
おわり
‘ω’)ノシ
コメントを残す