äºæ¥éã»ã©éä¸ãã¦goè¨èªã§ã³ã¼ãæ¸ããã®ã§ããã®éã«æãããã¨ãã¾ã¨ãã¦ã¿ã¾ããæ®æ®µã¯ã ãããPerlãJavaScriptã§Webã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã¦ãã¾ãã
ã¾ã¨ãã®è¦ç´
- goè¨èªè¯ã
- Webã¢ããªã±ã¼ã·ã§ã³æ¸ããªãPerlã¨ãRubyãè¯ã
- Perlã¨ãRubyæ¸ãã人ãããã«ã¦ã§ã¢æ¸ããªãgoè¨èªè¯ã
æ°ã«å ¥ã£ãã¨ãã
ã³ã³ãã¤ã«ã¨ã©ã¼ã親å
ãã¨ãã°æ°æ¥½ãªæ°æã¡ã§ä»¥ä¸ã®ãããªã³ã¼ããæ¸ãã¨
package main import ("fmt"; "net/http"; "log") func main() { resp := http.Get("http://hatenablog.com/") fmt.Println(math.Pi) }
以ä¸ã®ããã«ä¸å¯§ã«åé¡ç®æãæãã¦ããã¾ãã
./hoge.go:6: imported and not used: "log" ./hoge.go:10: multiple-value http.Get() in single-value context ./hoge.go:11: undefined: math
10è¡ç®ã§ããã§ã«ããããã¦ã11è¡ç®ã®ã¨ã©ã¼ãæãã¦ããã¾ãããã®æ®µéã§è¡¨ç¤ºãããã¨ã©ã¼ãå ¨é¨ãªããã¨ããã®ãã¡ã¤ã«ã«é¢ãã¦ã¯ã ãããã³ã³ãã¤ã«ãéãã®ã§ãã¡ãã£ã¨ãªããã¦ã¯ã³ã³ãã¤ã«ãã¦æ§åãã¿ã¦ã¾ããªãããã¨ãããã¨ããã¾ãããå¿ è¦ããªãã®ãè¯ãã§ããã
以ä¸è¿½è¨
http://t.co/V777wL4ivA
"ã³ã³ãã¤ã«ã¨ã©ã¼ã親å"
ã®ãããããªã«ã親åãªã®ãå
¨ãããããªãã£ããã ãã©ã
"10è¡ç®ã§ããã§ã«ããããã¦ã11è¡ç®ã®ã¨ã©ã¼ãæãã¦ããã¾ã"
ã£ã¦å½ããåã ã¨æã£ã¦ããã©ãããããªãã®ï¼
— Kenji Yoshida (@xuwei_k) 2013, 12æ 22
ãããã«ï¼
æ®æ®µPerlãRubyã使ã£ã¦ãã¨ãã©ããã¦ããã®è¡ãå®è¡ãããã¾ã§ããããªããæå³çã¨ã©ã¼ãèµ·ããå¾ã¾ããã¾ããã¨ã©ã¼ãçºçããå¾ã®é¨åã«ããã¨ã©ã¼ã¯ãã®æç¹ã§ã¯ããããªããã¨ãå¤ãã®ã§ãã³ã³ãã¤ã©ã®åå¨ã便å©ã«æãã¾ããã(Goã«éããã³ã³ãã¤ã©ã便å©)
ã³ã³ãã¤ã©ã®ããè¨èªã常ç¨ãã¦ãªãã®ã§ãä»ã®ã³ã³ãã¤ã©è¨äºæ¯è¼ãã¦ãã親åãªã®ãè©ä¾¡ããã®ãä¸å¯è½ã§ãããã¨ã©ã¼ã®å 容ãå ·ä½çã§ããããããã¨æãã¦ä½¿ã£ã¦ãã¾ãããåã«åãããã®ã§ãæ å ±éãå¤ãã¦ããããããã£ãã¨æãã¦ããã®ããããã¾ããã
ã¨ã©ã¼å¦çãå¿ãã«ãã
goè¨èªã§ã¯ä¸è¬çã«è¿ãå¤ã®äºçªç®ã«ã¨ã©ã¼ãè¨å®ããã¾ããã³ã¼ãã®åé¢ã«ã¨ã©ã¼å¤æ°ãç¾ããã®ã§ã¡ããã¨ã¨ã©ã¼å¦çããããã¨ããæ°åã«ãªãã¾ãããããããerrå¤æ°ãä¸åº¦ãå©ç¨ãã¦ããªãã¨ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãã
file,err := os.Open("/tmp/hoge")
ã¨ã ãæ¸ãã¦ããã¨ã以ä¸ã®æ§ãªã¨ã©ã¼ãåºã¾ãã
./hoge.go:8: err declared and not used
ã¨ã©ã¼å¦çã¯ã¨ãããã°è¦è½ã¨ããã¡ã§ãããå¿ããæ¸ãããèªå°ãããè¨èªè¨è¨ã«ãªã£ã¦ããã®ãè¯ãã§ããã
_ å¤æ°ãã¤ããã¨ç¡è¦ã§ãã¾ãããããã«ãã¨ã©ã¼ãç¡è¦ãã¦ããã¨ããã³ã¼ãã®åé¢ã«ãªãã®ã§ãã¬ãã¥ã¼ã§æããããã§ããããªããäºæ ããã£ã¦ç¡è¦ãã¦ã¦ãæ°ã¥ãããããã§ãã
resp,_ := http.Get("http://hatenablog.com/") // ããããã¨ã©ã¼å¦çãã¦ãªã...
ã¾ããæ»ãå¤ãåãåããªãã¨ã¨ã©ã¼ã«ãªãã¾ãã
resp := http.Get("http://hatenablog.com/") // ï¼ã¤ç®ã®æ»ãå¤ãç¡è¦
./hoge.go:9: multiple-value http.Get() in single-value context
ã¨ã©ã¼å¤æ°ã使ãã¾ããã¨ãã¨ã©ã¼å¦çãæãããã¨ã¯ããã®ã§æ°ãã¤ããå¿ è¦ã¯ããã¾ãã
file1,err := os.Open("/tmp/no_such_file") // ããã§èµ·ããã¨ã©ã¼ã¯å¦çããã¦ãªã file2,err := os.Open("/tmp/hoge") if err != nil { fmt.Println("error") } file1.Close() file2.Close()
以ä¸è¿½è¨ã
æ¦ãåæããã ã¨ã©ã¼ã«ã¤ãã¦ã¯ããæ»ãå¤ãåããªãã¦ãã³ã³ãã¤ã«è½ã¡ãªããã¨ããç¹ã¯æ³¨æãå¿
è¦ã(ãããç¥ãä»çµã¿ãä»ã¯ãªã) / âGoè¨èªã®æ°ã«å
¥ã£ãã¨ãã/æ°ã«å
¥ããªãã£ãã¨ãã - ã¯ãã¹ããã° â¨â http://t.co/RXhSEMnyPL
— Jxck (@Jxck_) 2013, 12æ 21
@hakobe æ£ç¢ºã«ã¯ãã¨ã©ã¼1å¤ããè¿ããªãé¢æ°ã§ããã§ãããä¾ãã° os ã«ã¯ã¨ã©ã¼ã®ã¿è¿ãé¢æ°ãããã¤ããã£ã¦ããããã¯æ»ãå¤ãåããªãã³ã¼ããæ¸ãã¦ããã³ã³ãã¤ã«ã¯éãã¾ãã
— Jxck (@Jxck_) 2013, 12æ 21
ãããã«!
range便å©
For文の言語仕様ã«rangeã使ãã¨ã©ããããã¨ãèµ·ããã®ãæ¸ãã¦ããã®ã§è¦ãã§ãã¯ã§ããé«æ©è½ã§ãã
goroutineã¨channel便å©
ï¼ã¤ã®çµã¿åããã§ã並åå¦çãéåæå¦çã®ã®è¨è¿°ãæ°æ¥½ã§ããä»åãããããªgoroutineã®ãã¿ã¼ã³ãæ¸ããã®ã§ãå¥ã®è¨äºã§ç´¹ä»ãããã
ã¯ãã¹ã³ã³ãã¤ã«ã便å©
ç°¡åã«ã¯ãã¹ã³ã³ãã¤ã«ã§ããã®ã§ä¾¿å©ã§ããä»åã¯Linuxã§ä½¿ããã¨ãæ³å®ãã¦ãã³ã¼ããæ¸ãã¦ããã®ã§ãããéçºç°å¢ã¯Macã ã£ãã®ã§ãMacã§Linuxç¨ã®ãã¤ããªããã«ãããããã¦ã¾ãããGoはクロスコンパイルが簡単 - unknownplace.org ã大å¤åèã«ãªãã¾ããã
æ°ã«å ¥ããªãã£ãã¨ãã
ãªã¹ãå¦çãé¢æ°åè¨èªã®ããã«æ¸ããªã
ãªã¹ãã®ãããããã£ã«ã¿å¦çãããå¦çãèãã¾ããtrivialãªä¾ã§ãããæ°å¤ã®ãªã¹ããã5以ä¸ã®æ°å¤ãã¨ãã ãããããè¾æ¸ã«å¤æããå¦çãPerlã§æ¸ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
my @maps = map { +{ value => $_} } grep { $_ < 5 } (0, 1, 2, 3, 4, 5)
åçã®ã³ã¼ããgoè¨èªã§æ¸ãã¨ä»¥ä¸ã®æ§ãªæç¶ãå¿åãªã¹ã¿ã¤ã«ã«ãªãã¾ãã
maps := make([]map[string]int) for _,v := range []int{0,1,2,3,4,5} { if ! v < 5 { continue } maps = append(maps, map[string]int{"value" : v}) }
åã¯Perlã®ãããªé¢æ°åè¨èªã¹ã¿ã¤ã«ã®ã»ãã宣è¨çã§èªã¿ãããã¦å¥½ããªã®ã§ãgoè¨èªã§ããããããæ¸ãæ¹ãã§ããã°ããããã£ãã
ã¨ã¯ãããã¹ã©ã¤ã¹ã«å¯¾ãã¦ä½¿ããé¢æ°ãã¡ã½ããã®ãããªãã®ãå¢ããã¨ã·ã³ãã«ãªè¨èªä»æ§ã¨ããgoè¨èªã®å©ç¹ã失ãããããgoè¨èªã«genericsããªããããããã¯ã®ä»£ããã«æ¸¡ãé¢æ°ã®åã®è§£æ±ºãé£ãããã§ããå®ç¾ã¯ç°¡åã§ãªãã®ããããã¾ããã
genericsããªã
ããã¯goè¨èªçãªFAQã§å ¬å¼ãµã¤ãã«ãã®ã£ã¦ã¾ããã(åè: http://golang.org/doc/faq#generics) å®éã«å¿ è¦æ§ãæããã®ã¯ä»¥ä¸ã®æ§ãªå ´åã§ãã
Show ã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã«æºæ ãã¦ãããªã¹ãã渡ãã¨é£çµãã¦ããã¨ããé¢æ°ãå®è£ ããããã«ããããã«ä»¥ä¸ã®ããã«æ¸ãã¦ã¿ã¾ããã
package main import ( "fmt" ) type Show interface { Show() string } type ConcretShow string func (c ConcretShow) Show() string { return string(c) } func showAll( shows []Show ) string { result := "" for _,s := range shows { result += s.Show() } return result } func main() { str := showAll( []ConcretShow { ConcretShow("hello, "), ConcretShow("world") } ) fmt.Println(str) }
ãããããã®ã³ã¼ãã¯ä»¥ä¸ã®æ§ãªã¨ã©ã¼ãçºçããããåä½ãã¾ããã
./hoge.go:26: cannot use []ConcretShow literal (type []ConcretShow) as type []Show in function argument
goã®ã¹ã©ã¤ã¹ã®åã¯å ±å¤ã§ã¯ãªããã§ãããJavaã®é åã®åã¯å ±å¤ãããã§ãããé åã®è¦ç´ ã¸ã®ä»£å ¥æã«ã©ã³ã¿ã¤ã ã¨ã©ã¼ãçºçããåå ã«ãªã£ã¦ãããããã¦ãããã¾ããã¾ããã£ã¦ãªããããã§ã(åè:http://www.ne.jp/asahi/hishidama/home/tech/java/array.html#h2_covariant) *1ã確ãã«ãã¨ããæãã
ããã§ãããgenericsçãªãã®ãããã°ã以ä¸ã®ããã«æ¸ãããã§ã (è¨æ³ã¯é©å½)ã
func showAll[T <: Show]( shows []T ) string { result := "" for _,s := range shows { result += s.Show() } return result }
å®éã«ãä¸è¨ã®æ§ãªè¨æ³ã¯ãªãã®ã§ empty interfaceãæ´»ç¨ãã¤ã¤type assertionã§Showåã«å¤æãã¾ãã
func showAll( shows []interface{} ) string { // interface {} ã®ã¹ã©ã¤ã¹ãåãåã result := "" for _,s := range shows { result += s.(Show).Show() // Showå㸠} return result } func main() { str := showAll( []interface{} { ConcretShow("hello, "), ConcretShow("world") } ) fmt.Println(str) }
type assertionã«ããã©ã³ã¿ã¤ã ã¨ã©ã¼ã®å¯è½æ§ãæ®ãã¾ãããããã¾ãåä½ããããã«ãªãã¾ãããå ã»ã©ã®goã®FAQã§ããinterface {} ãæ´»ç¨ããã°ãªãã¨ããªãã¨æ¸ããã¦ãã¾ããã
genericsãæ¸ããã»ããèªç¶ã§å ç¢ãªæ°ããã¾ãããè¨èªä»æ§ãå®è£ ã®è¤éããé«ããªãããã§ãé£ããã®ããããã¾ããã
JSONã®ãã¼ã¹ããã¸ã
ãªããããã¸ãã§ãããäºåã«JSONã«å¯¾å¿ããæ§é ä½ãæºåããããtype assertionãtype checkããã¦å°ãã¥ã¤å¤ãåãåºãå¿ è¦ãããã¾ã(åè: http://blog.golang.org/json-and-go)ãéççä»ãã®è¨èªã§ã¯ã©ã®ã¿ã¡å¤§å¤ããã
ã¾ã¨ã
goè¨èªãã¡ãã£ã¨æ¸ãã¦ã¿ãçµæã®ææ³ããæ°ã«å ¥ã£ãã¨ãããæ°ã«å ¥ããªãã£ãã¨ããã¨ããè¦ç¹ã§ã¾ã¨ãã¾ããã
å ¨ä½çã«ã¯ãããã¾ã§ã®ããã°ã©ãã³ã°è¨èªã®åé¡ç¹ããã¾ã解決ãã¤ã¤ããã·ã³ãã«ãªè¨èªä»æ§ã«ãªã£ã¦ãã¦ãçãè¯ããªã¨ããå°è±¡ã§ãããããã«è¦ãããã¦ãã»ã©ã»ã©ã«æ¸ããããã§ãããé©å½ã«æ¸ãã¦ãã¦ãã³ã³ãã¤ã©ããã§ãã¯ãã¦ãããã®ã§ãã³ã³ãã¤ã«ãã¨ããã°ãã¨ã©ã¼å¦çãªã©ãæããããå質ã®ã³ã¼ãã«ãªã£ã¦ãã¾ãã
ãã ãåçåä»ãã§ããã¯ã¿ã¤ãã³ã°æé«!ã¨ããæååã§ããã¾ã§ã³ã¼ããæ¸ãã¦ããã®ã§ããããä¸èªç±ã«æããé¨åãããã¾ããã
Webã¢ããªã±ã¼ã·ã§ã³ã®ããã³ãã«è¿ã(ããµã¼ããµã¤ãã®)ã½ããã¦ã§ã¢ã¯ãæ¯è¼çæ©è½ãåç´ã§ããã ãå¤æ´ããããã¨ãå¤ããã¾ãä¸åº¦ã«å®è¡ãããæéãçã(ä¸åã®HTTPãªã¯ã¨ã¹ãã§ããã°æ°ç§ä»¥å )ã¨ããç¹å¾´ãããã¾ãããããã£ãã½ããã¦ã§ã¢ãä½ãå ´åã¯ãgoè¨èªããããããæè»ãªRubyãPerlã®ãããªè¨èªãå©ç¨ãã¦çç£æ§ãéè¦ããã®ãè¯ãããã«æãã¾ããã
ä¸æ¹ãé·æéåä½ãå¤æ´é »åº¦ãæ¯è¼çä½ãããã«ã¦ã§ã¢å±¤ã®ã½ããã¦ã§ã¢ãè¨è¿°ããã®ã«ãgoè¨èªã¯ã´ã£ããã ã¨æãã¾ããããã«ã¦ã§ã¢å±¤ã§ã¯ãgoroutineãªã©ã®ä¸¦åå¦çæ©æ§ãæç¨ã«åãããã§ããWebã¢ããªã±ã¼ã·ã§ã³ã¨ã³ã¸ãã¢ãç¿å¾ã容æã§ãããæ¸ããããã«ãªã£ã¦ããã¨ä¾¿å©ããããã¾ãããã
*1:Scalaã®Listã¯å ±å¤ã ãã©immutableã ããåé¡ãªã