2017-05-09 54 views
0

這裏是golang的新功能。試圖按照sample code並創建下面的代碼片段,但我不斷收到編譯錯誤。不知道爲什麼。配置http客戶端時不斷收到編譯錯誤

go run te2.go 
# command-line-arguments 
./te2.go:36: syntax error: unexpected semicolon or newline, expecting comma or } 

這裏是代碼片段

package main 

import "fmt" 
import "bufio" 
import "os" 
import "time" 
import "net/http" 
import "sync/atomic" 

var req = []byte("GET /small HTTP/1.1\r\n" + 
"Host: localhost\r\n" + 
"Content-Length: 0\r\n\r\n"); 
var buf = make([]byte, 1024) 
var total uint64 = 0; 
var t0 = time.Now() 
var c = make(chan int) 

type DialerFunc func(net, addr string) (net.Conn, error) 

func make_dialer(keepAlive bool) DialerFunc { 
    return func(network, addr string) (net.Conn, error) { 
     conn, err := (&net.Dialer{ 
      Timeout: 3 * time.Second, 
      KeepAlive: 3000 * time.Second, 
     }).Dial(network, addr) 
     if err != nil { 
      return conn, err 
     } 
     conn.(*net.TCPConn).SetLinger(0) 
     return conn, err 
    } 
} 

func httpGet() int { 
    tr := &http.Transport{ 
     Dial: make_dialer(false) 
    } 
    client := &http.Client{ Transport: tr} 
    resp, err := client.Do(req) 
    //defer resp.Body.Close() 
    if (err != nil) { 
     fmt.Println(err) 
    } else { 
     resp.Body.Close() 
    } 
    atomic.AddUint64(&total, 1) 
    if (total == 10000) { 
     fmt.Println(time.Now().Sub(t0)) 
    } 
    c <- 1 
    return 0; 
} 

func main() { 
    i := 1 
    t0 = time.Now() 
    for (i < 10) { 
     go httpGet() 
     i += 1 
    } 
    for (1 < 2) { 
     <-c 
     go httpGet() 
    } 
    reader := bufio.NewReader(os.Stdin) 
    text, _ := reader.ReadString('\n') 
    fmt.Println(text) 
} 
+1

你忘了把逗號線36 - >'撥打:make_dialer(假),' –

回答

2

由於錯誤說,你做語法錯誤。
更具體地說,你已經忘記了把逗號在線36:

... 
func httpGet() int { 
    tr := &http.Transport{ 
     Dial: make_dialer(false), // <-- comma 
    } 
...