goè¨èªsnippetã¨æ³¨æç¹
以åæ¸ããpythonicãªA*é¢æ°ãã¾ã¾goã§æ¸ãã¦ã¿ã¾ããã
- astar.go: http://gist.github.com/233012
- astar_main.go: http://gist.github.com/233013
- åèpythonç: http://gist.github.com/147645
ãã®ã¨ã調æ»ã§ç解ãããã®ãç´¹ä»ãã¾ãã
0å¤
goã®0å¤(åæå¤ãè¨å®ããªãã¨ãã®ããã©ã«ãå¤)ãã©ããªãããããã¦ã©ãæ¯è¼ãããã®ä¾ã§ãã
package main import "fmt" type location struct { x, y int } func main() { var v location; fmt.Printf("%v\n", v); //fmt.Printf("%v\n", v == location{}); // not allowed fmt.Printf("%v\n", v.x == 0 && v.y == 0); var a *location; fmt.Printf("%v\n", a == nil); var b []location; fmt.Printf("%v\n", b); fmt.Printf("%v\n", b == nil); var c string; fmt.Printf("%v\n", c); fmt.Printf("%v\n", c == ""); var d map[string]float; fmt.Printf("%v\n", d); fmt.Printf("%v\n", d == nil); }
大ä½äºæ³éãã§ãããã ãstructå士ã¯==ã§çµã¹ã¾ãããåã¡ã³ãã¼ã0å¤ãã©ãããæ¯è¼ãããã¨ã«ãªãã§ããããã
nilã®ã¡ã½ããå¼ã³åºã
structã®ãã¤ã³ã¿ã¯nilã§ãã¡ã½ãããå¼ã³åºãã¾ãã
package main import "fmt" type foo struct { val int; } func (obj *foo) IsNil() bool { return obj == nil; } type nilable interface { IsNil() bool; } func main() { var o *foo; fmt.Printf("%v\n", o.IsNil()); //var i nilable; //fmt.Printf("%v\n", i.IsNil()); }
interfaceãnilã®å ´åãã¡ã½ããã解決ã§ããã¨ã©ã¼ãèµ·ãã¦æ»ã«ã¾ã(ã³ã¬ã¯æ£ããåããªã®ã§ããããï¼0å¤è¿ãã¹ããããªãããã¨ãæããã©)ã
for rangeã«ã¼ã
rangeã«ã¼ãã§ä½ãè¿ããã調ã¹ã¾ããã
package main import "fmt" func main() { s := "abcdef"; for _, c := range s { fmt.Printf("%f\n", c); fmt.Printf("%f\n", c == 'd'); } a := [...]string { "abc", "def", "ghi", }; for _, l := range a { fmt.Printf("%f\n", l); fmt.Printf("%f\n", l == "ghi"); } m := map[string]int { "abc": 10, "def": 20, "ghi": 30, }; for k, v := range m { fmt.Printf("%f\n", k); fmt.Printf("%f\n", v); fmt.Printf("%f\n", v == 20); } g := func(size int) <-chan int { ch := make(chan int); go func () { for i := 0; i < size; i++ { ch <- i * 2; ch <- i * 2 + 1; } close(ch); }(); return ch; }; for v := range g(3) { fmt.Printf("%f\n", v); } }
- æååã¨é åã¯ãã¤ã³ããã¯ã¹ã¨è¦ç´
- ãããã¯ããã¼ã¨å¤
- channelã¯ãå¤ã®ã¿
æå¾ã®ã¯pythonã®generator風ã«goroutineã使ã£ã¦ãã¾ãã
def g(size): for i in range(size): yield i * 2 yield i * 2 + 1 for v in g(3): println(v)
channelã®å
- "chan T"ãchannelã®åã§ããmakeã§ä½¿ãããéåä¿¡å¯è½ã§ãã
- "<-chan T"ã¯åä¿¡å°ç¨ã®channelåã§ãã
- ã¤ãã¬ã¼ã¿çã«channelãç¨ããå ´åããã®åãè¿ãã¨ããã§ãããã
- "chan<- T"ã¯éä¿¡å°ç¨ã®channelåã§ãã
ch := make(chan T); // type(ch) == chan T var receiver <-chan T = ch; var sender chan<- T = ch;
æ®éã¯éä¿¡ãåä¿¡å°ç¨ã¨ãã¦åã渡ããã¦ãããã®ãããããããã§ãããã
vectorã®ä½¿ãæ¹
package main import "fmt" import "container/vector" type ilocation interface { String() string; } type location [2]int func (loc *location) String() string { return fmt.Sprintf("%v", *loc); } func main() { v1 := vector.New(0); // v := vector.New(len) => v.Len() == len v1.Push(&location{5, 20}); v1.Push(&location{50, 10}); fmt.Printf("%v\n", v1); v1.Do(func (elem vector.Element) { fmt.Printf("%v\n", elem); }); v1.Do(func (elem vector.Element) { fmt.Printf("%v\n", elem.(ilocation).String()); }); for _, elem := range v1.Data() { fmt.Printf("%v\n", elem.(*location).String()); } for _, elem := range v1.Data() { fmt.Printf("%v\n", elem.(ilocation).String()); } // ptr array cast not allowed? //for _, elem := range v1.Data().([]ilocation) { // fmt.Printf("%v\n", elem.String()); //} v2 := v1.Slice(0, v1.Len()); // copy v2.Set(0, &location{0, 0}); fmt.Printf("%v\n", v1); fmt.Printf("%v\n", v2); }
- vector.Newã®å¼æ°ã¯æåã®ãµã¤ãºã§ãããã®ä¸ã¯ããããnilã«ãªãã¾ãã
- ã«ã¼ãã¯Doã§ã¯ãã¼ã¸ã£ãåãããã®ã¨ãDataã§é
åãã¨ãã ãã¦forã«ã¼ãããã®ã¨äºéãå¯è½ã§ãã
- é åã¯type assertion(ãã¤ãããã¯ãã£ã¹ãã®ãããªãã®)ã§ãã¾ããã
- Sliceã¡ã½ããã¯ãJavaScriptã®ããã«é åãã³ãã¼ãã¦ããã¾ã(sliceåã¨éã)ã
åå¤æ
intããfloat64ã¸ãªã©ã®åå¤æã¯ã以ä¸ã®ããã«è¡ãã¾ããå¤ãã®å ´åãæé»ã«å¤æããã¾ããã
a := 10; // int b := float64(a); // float64
heapããã±ã¼ã¸
pythonã®heapqã®ãããªãã¨ãã§ããheapããã±ã¼ã¸ãããã¾ãã
ãã ãããã¥ã¼æ¬ä½ã«ãªãããã«å¿
è¦ãªheap.Interfaceã®ã¡ã½ãã群ã¯ãVectorãã»ã¼åããã®ãæã£ã¦ããã®ã§ãããåãªã©ã«å¾®å¦ã«éãããããããã¤ã³ã¿ãã§ã¼ã¹ãããããããå¿
è¦ãããã使ãæ¹ã¯è¥å¹²è¤éã§ãã
import "fmt" import "container/vector" import "container/heap" type scored struct { score float; data string; } type heapq struct { vector.Vector; } func (queue *heapq) Init(len int) *heapq { queue.Vector.Init(0); return queue; } func (queue *heapq) At(i int) *scored { return queue.Vector.At(i).(*scored); } func (queue *heapq) Less(i, j int) bool { return queue.At(i).score < queue.At(j).score; } func (queue *heapq) Push(o interface {}) { queue.Vector.Push(o.(vector.Element)); } func (queue *heapq) Pop() interface {} { return queue.Vector.Pop(); } func main() { queue := new(heapq).Init(0); println("ok0"); heap.Init(queue); println("ok1"); heap.Push(queue, &scored{10.0, "go"}); println("ok2"); heap.Push(queue, &scored{5.0, "java"}); heap.Push(queue, &scored{7.0, "python"}); heap.Push(queue, &scored{1.0, "c++"}); heap.Push(queue, &scored{3.0, "c"}); println("ok3"); for queue.Len() > 0 { data := heap.Pop(queue).(*scored); fmt.Printf("%v\n", data); } println("ok4"); }
Vectorã®è¦ç´ ããvector.Elementãããªããinterface{}ã ã£ããPush/Popã®ä¸æ¸ããããªãã®ã«ã
newã¨Composite literal
type Foo struct { .... }
ã¨ããåã«ã¤ãã¦ãäºã¤ã®ã¤ã³ã¹ã¿ã³ã¹çæå¼
&Foo{}
ã¨
new(Foo)
ã¯ã¾ã£ããåããã®ã§ããåè ãã¹ã¿ãã¯ä¸ã«è©°ã¾ããã¨ããã«ã¼ãä¸ã§ã¡ã¢ãªé åãåå©ç¨ãããã¨ãããããããã¨ã¯ããã¾ããã
åè ã®ãªãã©ã«å½¢å¼(ããåç §ãå¾ãè¨æ³)ã§ã¯ãåæãã¼ã¿ãå ¥ããããããã¦ä¾¿å©ã§ãããããããã®ãªãã©ã«å½¢å¼ã§ã¯çæã§ããªãåãããã¾ãããã¨ãã°ãã¤ã³ã¿åã¨ããããããå ´åã«ã¯newããæ段ãããã¾ããã
課é¡
pythonã¨goã¨ã§ã®A*æ¢ç´¢ãæ¸ãã¦ããã¹ã¦ã¿ãã¨ãå¢ãã¦ãé¨åã®å¤ããçµã¿è¾¼ã¿Tupleãç¡ããã¨ã«ç±æ¥ããæãããã¾ãã
tupleçãã¢ãä½æããvectorã®å¤§å°æ¯è¼ãåå®è£
ããããã¾ããããã¯ãpythonã®tupleã§ã¯èªç¶ã«åãã£ã¦ãæ©è½ãåå¤å¤å®ã大å°å¤å®ãmapãã¼å©ç¨å¯è½æ§ãç¡ãããã ã¨æãã¾ãã
ã»ãã®å·®ã§ã¯ã
- comprehensionããªããã¡ã¢ãªç¢ºä¿å¾ã«ã«ã¼ãããªãã¦ã¯ãããªããã¨
- ã¹ãä¹æ¼ç®åãç¡ãmath.Powãmath.Sqrtãã¤ãããªãã¦ã¯ãããªããã¨
- ãªãã©ã«ã¯floatã ãã©ãmathã®é¢æ°ã®å¼æ°ãçµæã§ã¤ãããã¦ãã®ã¯float64ã§ãããã¨
- ãªãã¬ã¼ã¿ãªã¼ãã¼ãã¼ãããªããããã¹ã³ã¢ãæ½è±¡åãããã¨ããã¨ãéã«åç´ãªæ°å¤ã使ãã«ãããªããã¨
- interfaceã«å¤ç¸åãã§ããªããããã³ã¼ã«ããã¯ã§type assertion(ãã¤ãããã¯ãã£ã¹ã)ãå¤ç¨ããªãã¦ã¯ãããªããã¨
ãªã©ãæ°ã«ãªã£ãã¨ããã§ããããããç¹ãé¤ãã¦ãCãJavaã«æ¯ã¹ãã°ã ãã¶ã³ã³ãã¯ãã«æ¸ããã¨æãã¾ããã
å¢ãã¦ãå¤ãã¯tuple代ããã«ç¨æããåã«é¢ä¿ãããã®ãªã®ã§ãgoã«æ £ããã°ããå°ãæ¸ãéãæ¸ãããããããã¾ãã(interfaceã«ã¸ã§ããªã¯ã¹ãå®è£ ãããã°ãtupleã©ã¤ãã©ãªãã§ãããã ãããã©...)ã