【问题标题】:keep getting compile error when configuring http client配置http客户端时不断出现编译错误
【发布时间】:2017-05-09 05:05:02
【问题描述】:

这里是 golang 的新手。尝试关注sample code 并创建了以下代码 sn-p,但我不断收到编译错误。不知道为什么。

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

这里是代码sn-p

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)   
}

【问题讨论】:

  • 你忘记在第 36 行加逗号 -> Dial: make_dialer(false),

标签: go


【解决方案1】:

正如错误所说,您犯了语法错误。
更具体地说,您忘记在第 36 行加上逗号:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多