Last active
December 28, 2015 18:09
-
-
Save kwmt/7540879 to your computer and use it in GitHub Desktop.
第1回大阪Go勉強会の課題をやってなかったので、いまさらながらやってみた。
This file contains hidden or 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" | |
"time" | |
) | |
func main() { | |
a := dayofyear(2012, 12, 31) | |
fmt.Println(a) | |
} | |
func dayofyear(year int, month int, day int) int { | |
m := time.Month(month) | |
base := time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC) | |
t := time.Date(year, m, day, 0, 0, 0, 0, time.UTC) | |
d := t.Sub(base) | |
return int(d.Hours()/24.0 + 1) | |
} |
This file contains hidden or 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() { | |
var i int | |
for { | |
i++ | |
switch { | |
case i%15 == 0: | |
fmt.Println(i, ":FizzBuzz") | |
case i%3 == 0: | |
fmt.Println(i, ":Fizz") | |
case i%5 == 0: | |
fmt.Println(i, ":Buzz") | |
} | |
if i == 100 { | |
break | |
} | |
} | |
} |
This file contains hidden or 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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
var totalprice int | |
var totalpages int | |
//テストデータ | |
var isbns = []string{"9784873115948", "9784873116402", "9784873114750"} | |
func main() { | |
book := make(chan Book) | |
quit := make(chan bool) | |
exit := make(chan bool) | |
for _, isbn := range isbns { | |
go BookInfo(isbn, book, quit) | |
} | |
go listenBook(book, quit, exit) | |
<-exit | |
fmt.Println("総ページ数:", totalpages, ",総額:", totalprice) | |
} | |
type Book struct { | |
Title string `json:"title"` | |
Pages int `json:"pages"` | |
Price int `json:"price"` | |
} | |
func BookInfo(isbn string, b chan Book, quit chan bool) { | |
resp, _ := http.Get("http://www.oreilly.co.jp/books/" + isbn + "/biblio.json") | |
body, _ := ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
var book Book | |
json.Unmarshal(body, &book) | |
b <- book | |
quit <- true | |
} | |
func listenBook(book chan Book, quit chan bool, exit chan bool) { | |
var i int | |
for { | |
select { | |
case b := <-book: | |
totalprice = totalprice + b.Price | |
totalpages = totalpages + b.Pages | |
case <-quit: | |
i++ | |
if i >= len(isbns) { | |
exit <- true | |
} | |
} | |
} | |
} |
This file contains hidden or 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 ( | |
"bufio" | |
"fmt" | |
"os" | |
"strings" | |
) | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
wcMap := make(map[string]int) | |
for scanner.Scan() { | |
text := scanner.Text() | |
fmt.Println(scanner.Text()) | |
words := strings.Split(text, " ") | |
for _, word := range words { | |
wcMap[strings.TrimSpace(word)]++ | |
} | |
for w, c := range wcMap { | |
fmt.Println(w, " : ", c) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment