Create a gist now

Instantly share code, notes, and snippets.

@shirayu /go-net0.go
Last active Aug 29, 2015

What would you like to do?
golang-sample
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
urls := []string{
"http://example.com",
"http://example.net",
"http://example.co.jp",
"http://example.org",
}
for _, url := range urls {
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
fmt.Printf("%s\t%s\n", url, res.Status)
}
}
package main
import (
"fmt"
"net/http"
)
func getStatus(urls []string) <-chan string {
statusChan := make(chan string)
for _, url := range urls {
go func(url string) { //匿名関数
res, err := http.Get(url)
if err != nil {
statusChan <- err.Error()
return
}
defer res.Body.Close()
statusChan <- fmt.Sprintf("%s\t%s", url, res.Status)
}(url)
}
return statusChan
}
func main() {
urls := []string{
"http://example.com",
"http://example.net",
"http://example.co.jp",
"http://example.org",
}
//内部で呼び出したゴルーチンの終了は待たずにメインスレッドは続行される
statusChan := getStatus(urls)
for i := 0; i < len(urls); i++ {
fmt.Printf("%s\n", <-statusChan)
}
}
package main
import (
"fmt"
"net/http"
"time"
)
func getStatus(urls []string) <-chan string {
statusChan := make(chan string)
for _, url := range urls {
go func(url string) { //匿名関数
res, err := http.Get(url)
if err != nil {
statusChan <- err.Error()
return
}
defer res.Body.Close()
statusChan <- fmt.Sprintf("%s\t%s", url, res.Status)
}(url)
}
return statusChan
}
func getStatusWithTimeout(urls []string) {
statusChan := getStatus(urls)
timeout := time.After(time.Second)
for {
select {
case status := <-statusChan:
fmt.Printf("%s\n", status)
case <-timeout:
return
}
}
}
func main() {
urls := []string{
"http://example.com",
"http://example.net",
"http://example.co.jp",
"http://example.org",
}
getStatusWithTimeout(urls)
}
package main
import (
"fmt"
"net/http"
"time"
)
func getStatus(urls []string) <-chan string {
statusChan := make(chan string, len(urls)) //URLの数だけバッファを確保
for _, url := range urls {
go func(url string) { //匿名関数
res, err := http.Get(url)
if err != nil {
statusChan <- err.Error()
return
}
defer res.Body.Close()
statusChan <- fmt.Sprintf("%s\t%s", url, res.Status)
}(url)
}
return statusChan
}
func main() {
urls := []string{
"http://example.com",
"http://example.net",
"http://example.co.jp",
"http://example.org",
}
statusChan := getStatus(urls)
for i := 0; i < len(urls); i++ {
fmt.Printf("%s\n", <-statusChan)
time.Sleep(time.Second) //時間のかかる処理
}
}
package main
import (
"fmt"
"net/http"
)
func getStatus(urls []string) <-chan string {
statusChan := make(chan string, len(urls)) //URLの数だけバッファを確保
var empty struct{}
limit := make(chan struct{}, 2) //同時ゴルーチン起動数=2
for _, url := range urls {
select {
case limit <- empty:
go func(url string) { //匿名関数
res, err := http.Get(url)
if err != nil {
statusChan <- err.Error()
return
}
defer res.Body.Close()
statusChan <- fmt.Sprintf("%s\t%s", url, res.Status)
<-limit //終わったら1つ読みだして空きを作る
}(url)
}
}
return statusChan
}
func main() {
urls := []string{
"http://example.com",
"http://example.net",
"http://example.co.jp",
"http://example.org",
}
statusChan := getStatus(urls)
for i := 0; i < len(urls); i++ {
fmt.Printf("%s\n", <-statusChan)
}
}
package main
import (
"bufio"
"fmt"
"github.com/jessevdk/go-flags"
"os"
)
type cmdOptions struct {
Help bool `short:"h" long:"help" description:"Show this help message"`
Input string `short:"i" long:"input"`
}
func main() {
opts := cmdOptions{}
optparser := flags.NewParser(&opts, flags.Default)
optparser.Name = ""
optparser.Usage = "-i input -o output [OPTIONS]"
optparser.Parse()
f := os.Stdin
if opts.Input != "" && opts.Input != "-" {
var err error
f, err = os.Open(opts.Input)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
defer f.Close()
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fmt.Printf("Got [%s]\n", scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment