英文:
keep getting compile error when configuring http client
问题
新手学习golang。尝试按照示例代码创建了以下代码片段,但是一直出现编译错误,不知道为什么。
go run te2.go
# command-line-arguments
./te2.go:36: 语法错误: 意外的分号或换行符,期望逗号或 }
以下是代码片段:
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)
}
英文:
New to golang here. Tried to follow sample code and created the following code snippet, but I keep getting compile error. Not sure why.
go run te2.go
# command-line-arguments
./te2.go:36: syntax error: unexpected semicolon or newline, expecting comma or }
Here is code snippet
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
得分: 2
根据错误提示,你的代码存在语法错误。
更具体地说,你在第36行忘记了加逗号:
...
func httpGet () int {
tr := &http.Transport{
Dial: make_dialer(false), // <-- 逗号
}
...
英文:
As error says, you have made syntax error.
More specifically, you have forgotten to put comma on line 36:
...
func httpGet () int {
tr := &http.Transport{
Dial: make_dialer(false), // <-- comma
}
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论