golang的新手。试图按照sample code创建以下代码片段,但我不断收到编译错误。不知道为什么。
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)
}
答案 0 :(得分:2)
正如错误所说,你犯了语法错误 更具体地说,你忘了把逗号放在第36行:
...
func httpGet () int {
tr := &http.Transport{
Dial: make_dialer(false), // <-- comma
}
...