Goè¨èªã®æ§é ä½ã«underscore.js風ã®ã¡ã½ããã追å ãããgenãã試ã
ã¤ã³ã¹ãã¼ã«
$ go get github.com/clipperhouse/gen
ã³ãã³ãã©ã¤ã³ãã¼ã«ãgenã
ã³ãã³ãã©ã¤ã³ãã¼ã«ãgenããã¤ã³ã¹ãã¼ã«ããã
$ gen Usage: gen [[*]package.TypeName] [-[*]all] [-exported] *package.TypeName # generate funcs for specified struct type; use leading * to specify pointer type (recommended) -all # generate all structs in current directory; use leading * to specify pointer type (recommended); shortcut -a or -*a -exported # only generate exported structs; shortcut -e -force # force generate, overriding errors; shortcut -f Examples: gen -*models.Movie # generates funcs for Movie type in the models package; generated Movies type is []*Movie gen -models.Movie # generates funcs for Movie type; generated Movies type is []Movie gen -*all # generates funcs for all struct types in current directory, as pointers gen -all # generates funcs for all struct types in current directory, as values gen -*a -e # generates funcs for all exported struct types in current directory, as pointers
使ã£ã¦ã¿ã
é©å½ãªãã£ã¬ã¯ããªã«main.goãç¨æãã¦ãåãç¨æã
SelectãAverageãªã©ãä¸é¨ã®ã¡ã½ããã使ãããã«ã¿ã°ãæ¸ãã¦ããå¿
è¦ãããã
package main type Person struct { Name string `gen:"Select"` Age int `gen:"Average,Sum"` } func main() { }
ä½æãããã£ã¬ã¯ããªã«cdãã¦genãå®è¡
$ gen main.Person generated main.Persons, yay!
ããã¨person_gen.goãã¡ã¤ã«ãä½æãããPersonså(Personã®ã¹ã©ã¤ã¹)ã«ãAllãAnyãªã©ã®æ§ã
ãªã¡ã½ãããç¨æãããã
person_gen.go
// gen main.Person // this file was auto-generated using github.com/clipperhouse/gen // Mon, 13 Jan 2014 05:26:10 UTC // Sort functions are a modification of http://golang.org/pkg/sort/#Sort // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main import ( "errors" ) // The plural (slice) type of Person, for use with gen methods below. Use this type where you would use []Person. (This is required because slices cannot be method receivers.) type Persons []Person // Tests that all elements of Persons return true for the passed func. See: http://clipperhouse.github.io/gen/#All func (rcv Persons) All(fn func(Person) bool) bool { for _, v := range rcv { if !fn(v) { return false } } return true } // 以ä¸çç¥...
ç¨æãããã¡ã½ããã使ã£ã¦ã¿ã
package main import "fmt" type Person struct { Name string `gen:"Select"` Age int `gen:"Average,Sum"` } func main() { //ããã¼ã¿ç¨æ clara := Person{"Clara", 14} tim := Person{"Tim B.", 21} jeremy := Person{"Jeremy", 12} julia := Person{"Julia", 18} emma := Person{"Emma", 20} people := Persons{clara, tim, jeremy, julia, emma} // 18以ä¸ã®Personã®ååãåå¾ names := people.Where(func(p Person) bool { return p.Age >= 18 }).SelectName() fmt.Println(names) // [Tim B. Julia Emma] // å¹´é½¢ã®å¹³å ave, err := people.AverageAge() if err != nil { panic(err) } fmt.Println(ave) // 17 // å¹´é½¢ã®åè¨ sum := people.SumAge() fmt.Println(sum) // 85 // å¹´é½¢ã§ã½ã¼ã byAge := func(a, b Person) bool { return a.Age < b.Age } fmt.Println(people.Sort(byAge)) // [{Jeremy 12} {Clara 14} {Julia 18} {Emma 20} {Tim B. 21}] }
ä»ã«ãããããç¨æããã¦ããã
gen - a generics library for Go
以å試ããgo-linqã¨ã¯ç°ãªããæ§é ä½ãã¨ã«ã¡ã½ãããçæããã®ã§ããã£ã¹ããããå¿ è¦ããªãã·ã³ãã«ã«ä½¿ããã®ãè¯ãæã