やってたこと
goは講義でも聞いたけど、触るのは初めて。
勉強会では、ツアーをして構文に慣れることから始めました。
触ってみての感想
変な感じの言語だなー、と。僕はCとRubyを主に使ってたので入りは楽でした。
どんな感じで変かというと、
- 型宣言が変(型宣言が後にくる)
- return文が変(最初に宣言するパターンとreturnに明示するパターン)
- for文とif文に()がなくて変(これカッコを入れるとエラーなんだぜ?)
- 構造体pointerが変(たまに変な挙動が)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
sum := 0 | |
for i := 0; i < 10; i++ { | |
sum += i | |
} | |
fmt.Println(sum) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func pow(x, n, lim float64) float64 { | |
if v := math.Pow(x, n); v < lim { | |
return v | |
} | |
return lim | |
} | |
func main() { | |
fmt.Println( | |
pow(3, 2, 10), | |
pow(3, 3, 20), | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func split(sum int) (x, y int) { | |
x = sum * 4 / 9 | |
y = sum - x | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func add(x, y int) int { | |
return x + y | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type Vertex struct { | |
X int | |
Y int | |
} | |
func main() { | |
v := Vertex{1, 2} | |
p := &v | |
p.X = 1e9 | |
fmt.Println(v) | |
} |
でも、便利なものもあって
- 型推論が便利(:=が便利)
- deferが便利(遅延して実行してくれるのは何かに役立ちそう)
- sliceが便利(初めての概念)
今回はできなかった並列分散のgo routin(?)は今度やってみます。
0 件のコメント:
コメントを投稿