golangè²ã
æãè¦ã¦Goè¨èªã試ãä¸ãé©å½ã«ç°¡åãããªããã±ã¼ã¸ã¨ãã使ããªããGoã£ã½ãæ¸ãæ¹ã«æ
£ãã¦ã¿ããã¨ã«ããã
ãªãã¸ã§ã¯ãã®åæåæ¹æ³è²ã
ãµã³ãã«ã³ã¼ãã¨ããè¦ã¦ããã¨ããªãã¸ã§ã¯ãçæã®æ¸ãæ¹ã¯ããã¤ããã£ã¦ãåå{ã} ã使ãæ¹æ³ãnew ã使ãæ¹æ³ãmake ã使ãæ¹æ³ãªã©ãããããã ãã¾ã ããããã®ä½¿ãæ¹ã¨ãç¹å¾´ã¨ãã¾ã ããåãã£ã¦ãªããã© åå{ã} ã®ä½¿ãæ¹ã¯ä½ã¨ãªãããã£ãã®ã§ã¡ã¢ã
âãããªæãã«æ¸ããããã ã
package main import ( "fmt"; ) type A struct { a int; b string; } func main() { //typeã§å®ç¾©ãããé çªã«åæå¤ãä»£å ¥ããæã var a1 A = A{1, "a1"}; //ãã£ã¼ã«ãåãæå®ããåæåãåºæ¥ã var a2 A = A{a:2, b:"a2"}; //ãã£ã¼ã«ãé ã¨ãã£ã¼ã«ãåæå®ãåæã«æ±ããã¨ã¯åºæ¥ãªãã¦ãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªã //var a3 A = A{3, b:"a3"}; //ãã¤ã³ã¿ã®å ´åã¯ãããªæãã§OK var a4 *A = &A{4, "a4"}; //newãããã¨ãåºæ¥ã模æ§ãnewã«ãã£ã¦ä½ãèµ·ããã®ãã®è©³ç´°ã¯ã¾ã ç解ãã¦ããªã(^^; var a5 *A = new(A); //varã¨åãæ¸ãã®ãé¢åãªå ´å㯠:= ã使ãæ¹ã楽 a6 := A{6, "a6"}; a7 := &A{7, "a7"}; //ãã¤ã³ã¿ã¯åæåããªãã¨nilãå ¥ã var a8 *A; //2次å é åã®åæåã¨ãã¯ã¡ãã£ã¨ãã¡ããã¡ãããæãã 㪠a9 := [][]int{[]int{1,2}, []int{11,22,33}}; //å¤æ°ã®å 容確èªã«ã¯ %#v ã便å©ï¼ fmt.Printf("%#v\n", a1); fmt.Printf("%#v\n", a2); //fmt.Printf("%#v\n", a3); fmt.Printf("%#v\n", a4); fmt.Printf("%#v\n", a5); fmt.Printf("%#v\n", a6); fmt.Printf("%#v\n", a7); fmt.Printf("%#v\n", a8); fmt.Printf("%#v\n", a9); }
âã®å®è¡çµæãã³ã¬â
main.A{a:1, b:"a1"} main.A{a:2, b:"a2"} &main.A{a:4, b:"a4"} &main.A{a:0, b:""} main.A{a:6, b:"a6"} &main.A{a:7, b:"a7"} (*main.A)(nil) [][]int{[]int{1, 2}, []int{11, 22, 33}}
ä¸å®æéæ¯ã«å¦çãç¹°ãè¿ãã«ã¯
timeããã±ã¼ã¸ã®time.NewTickerãtime.Tickã使ãã°è¯ãããã ã
NewTickerã¨Tickã¯ã©ã¡ããåããããªåããããé¢æ°ã ãæ»ãå¤ããã£ã³ãã«ã ããªã®ãTickerãã®éãããããå¾è
ã®æ¹ãStopã¡ã½ããã§Tickerã®å¶å¾¡ãåºæ¥ãåè¯ãæ°ããããå®è¡ééãããç§ã§æå®ãã¦Tickerãåãåãããã¨ã¯ticker.Cã¨ãããã£ã³ãã«ããTickerä½ææã«æå®ããæéééã§ããã®æç¹ã§ã®ç¾å¨æå»ã®ããç§ãåãåºãããã¨ããä»çµã¿ã®ããã ã
package main import ( "time"; "fmt"; ) func main() { //1ç§åä½ã®ãã£ãã«ã¼ãä½æãã ticker := time.NewTicker(1000*1000*1000); for { //ãã£ã³ãã«ããå¤ãåºã¦ããã®ãå¾ ã¤ ns := <-ticker.C; t := time.SecondsToLocalTime(ns /1000/1000/1000); fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second); } }
âã®å®è¡çµæãã³ã¬â
2009-11-19 02:22:19 2009-11-19 02:22:20 2009-11-19 02:22:21 2009-11-19 02:22:22 2009-11-19 02:22:23 :
ããã§ã¯ãã£ã³ãã«ã®ä½¿ãæ¹ã®ã²ã¨ã¤ãç解ããã
ãã£ã¬ã¯ããªãå帰çã«è¾¿ã£ã¦å¦çãããã«ã¯
pathããã±ã¼ã¸ã®path.Walkã使ãã°è¯ãããã ã
path.Walk ã¯å¼æ°ã« Visitor ã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãåãã®ã§ããããå®è£
ããã¿ã¤ããä½ã£ã¦å¼æ°ã«æ¸¡ããã¨ã§ find ã®ãããªå¦çãåºæ¥ãããã«ãªãããããã¨ããããé©å½ã«ä½¿ã£ã¦ã¿ãã
package main import ( "fmt"; "path"; "os"; ) type SimpleVisitor struct { } func (v *SimpleVisitor) VisitDir(path string, d *os.Dir) bool { fmt.Printf("d: %s\n", path); return true; } func (v *SimpleVisitor) VisitFile(path string, d *os.Dir) { fmt.Printf("f: %s\n", path); } func main() { v := &SimpleVisitor{}; errors := make(chan os.Error); path.Walk("/etc/httpd", v, errors); }
âããã®å®è¡çµæã¯ã³ã¬â
d: /etc/httpd d: /etc/httpd/conf f: /etc/httpd/conf/httpd.conf f: /etc/httpd/conf/magic d: /etc/httpd/conf.d f: /etc/httpd/conf.d/README f: /etc/httpd/conf.d/mod_dnssd.conf f: /etc/httpd/conf.d/php.conf f: /etc/httpd/conf.d/proxy_ajp.conf f: /etc/httpd/conf.d/welcome.conf f: /etc/httpd/logs f: /etc/httpd/modules f: /etc/httpd/run
ã»ã¼äºæ³éãã®åä½ã ãã¡ãªã¿ã«VisitDirã§falseãè¿ãã¨ãã®ãã£ã¬ã¯ããªã®ä¸ã¯è¾¿ããªããªããã¾ãã·ã³ããªãã¯ãªã³ã¯ã¯VisitFileã®æ¹ã§æ±ãããã®ã§ã·ã³ããªãã¯ãªã³ã¯ã®ãã£ã¬ã¯ããªã辿ãããå ´åã¯VisitFileå
ã§dir.IsSymlink()ã¨ãdir.IsDirectory()ããã§ãã¯ãã¦ãããã¯èªåã§path.Walkãæ¹ãã¦å®è¡ãã¦ããå¿
è¦ã¯ããããã ã
ããã§ã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£
æ¹æ³ã«ã¤ãã¦ç解ã§ããæ°ãããã
ããã°ã©ã ã®çµäºæ¹æ³è²ã
golang/éå¼ã/ããã°ã©ã ã®çµäºæ¹æ³è²ã
- main ãreturnããã ã
- os.Exit(code int) ã使ã
- panic()ãpanicln() ã使ã
ãªã©ã使ããããã§ãã
ã¯ã¦ãã§ãã¤ã©ã¤ãåºæ¥ãªããã¨ã«ä¸æº
ã¯ã¦ãªã¢ã¤ã㢠- ã¹ã¼ãã¼ãã¬è¨æ³ã§Googleçºã®ããã°ã©ã è¨èªãGoãã®ãã¤ã©ã¤ãã使ããããã«ãã¦ã»ãããhttp://golang.org/
ã¨ããããã¢ã¤ãã¢ã¨ãã¦åºãã¦ã¿ãã
追è¨ï¼å®è£ ãããï¼