Skip to content

Instantly share code, notes, and snippets.

@m0sth8
Last active August 29, 2015 14:06
Show Gist options
  • Save m0sth8/81a25f7b4d1f8c5c21ab to your computer and use it in GitHub Desktop.
Save m0sth8/81a25f7b4d1f8c5c21ab to your computer and use it in GitHub Desktop.
cpp_party
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Response struct {
Args map[string]string `json:"args"`
}
func main() {
urls := []string{
"http://httpbin.org/get?id=1",
"http://httpbin.org/get?id=2",
"http://errordomain/get?id=2",
}
for i, url := range urls {
if resp, err := http.Get(url); err != nil {
fmt.Println(err)
} else {
var data Response
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
fmt.Println(err)
} else {
resp.Body.Close()
fmt.Printf("%d: %#v \n", i, data)
}
}
}
}
start := time.Now()
for i, url := range urls {
go func(i int, url string){
...
}(i, url)
}
time.Sleep(time.Second * 2)
fmt.Printf("Time[all]: %v \n", time.Now().Sub(start))
>>
2: Get http://errordomain/get?id=2: dial tcp: lookup errordomain: no such host
Time[2]: 1.561192ms
1: main.Response{Args:map[string]string{"id":"2"}}
Time[1]: 393.452145ms
0: main.Response{Args:map[string]string{"id":"1"}}
Time[0]: 395.384145ms
Time[all]: 2.000193162s
start := time.Now()
result := make(chan bool, len(urls))
for i, url := range urls {
go func(i int, url string, result chan bool){
...
result <- true
}(i, url, result)
}
for i, _ := range urls {
<- result
}
fmt.Printf("Time[all]: %v \n", time.Now().Sub(start))
>>
2: Get http://errordomain/get?id=2: dial tcp: lookup errordomain: no such host
Time[2]: 1.561192ms
1: main.Response{Args:map[string]string{"id":"2"}}
Time[1]: 393.452145ms
0: main.Response{Args:map[string]string{"id":"1"}}
Time[0]: 395.384145ms
Time[all]: 395.484145ms
0: main.Response{Args:map[string]string{"id":"1"}}
1: main.Response{Args:map[string]string{"id":"2"}}
Get http://errordomain/get?id=2: dial tcp: lookup errordomain: no such host
0: main.Response{Args:map[string]string{"id":"1"}}
Time[0]: 381.101278ms
1: main.Response{Args:map[string]string{"id":"2"}}
Time[1]: 249.843369ms
2: Get http://errordomain/get?id=2: dial tcp: lookup errordomain: no such host
Time[2]: 848.063us
Time[all]: 631.814517ms
func copyPart1(filename string) []byte {
b, _ := ioutil.ReadFile(filename) // []byte{...}
b = b[100:200]
return b
}
func copyPart2(filename string) []byte {
b, _ := ioutil.ReadFile(filename) // []byte{...}
b = b[100:200]
c := make([]byte, len(b))
copy(c, b)
return c
}
func Append(slice []int, elements ...int) []int {
n := len(slice)
total := len(slice) + len(elements)
if total > cap(slice) {
// Reallocate. Grow to 1.5 times the new size, so we can still grow.
newSize := total*3/2 + 1
newSlice := make([]int, total, newSize)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[:total]
copy(slice[n:], elements)
return slice
}
s := []int{1, 2, 3}
s = Append(s, 4, 5, 6)
type myInt int
func (mi myInt) String() string {
return fmt.Sprintf("myInt: %d", mi)
}
func main() {
fmt.Println(myInt(10)) // myInt: 10
}
type Stringer interface {
String() string
}
type Response struct {
Name string
}
func (r *Response) String() string{
if r == nil {
return fmt.Sprintf("Response: nil")
}
return fmt.Sprintf("Response: %s", r.Name)
}
type ExtraResponse struct {
*Response
}
func main() {
var (
resp *Response
resp2 *ExtraResponse
str fmt.Stringer
)
fmt.Println(resp) // Response: nil
resp = &Response{Name:"First"}
fmt.Println(resp) // Response: First
resp2 = &ExtraResponse{resp}
fmt.Println(resp2) // Response: First
str = resp
fmt.Println(str) // Response: First
}
start := time.Now()
for i, url := range urls {
start := time.Now()
if resp, err := http.Get(url); err != nil {
fmt.Printf("%d: %s\n", i, err)
} else {
var data Response
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
fmt.Println(err)
} else {
resp.Body.Close()
fmt.Printf("%d: %#v \n", i, data)
}
}
fmt.Printf("Time[%d]: %v \n", i, time.Now().Sub(start))
}
fmt.Printf("Time[all]: %v \n", time.Now().Sub(start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment