Skip to content

Instantly share code, notes, and snippets.

@shirayu
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirayu/c26b96a4a9c268cfb3c8 to your computer and use it in GitHub Desktop.
Save shirayu/c26b96a4a9c268cfb3c8 to your computer and use it in GitHub Desktop.
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