在配置HTTP客户端时一直出现编译错误。

huangapple go评论97阅读模式
英文:

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 &quot;fmt&quot;
import &quot;bufio&quot;
import &quot;os&quot;
import &quot;time&quot;
import &quot;net/http&quot;
import &quot;sync/atomic&quot;

var req = []byte(&quot;GET /small HTTP/1.1\r\n&quot; +
&quot;Host: localhost\r\n&quot; +
&quot;Content-Length: 0\r\n\r\n&quot;);
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 := (&amp;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 := &amp;http.Transport{
		Dial: make_dialer(false)
	}
	client := &amp;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(&amp;total, 1)
	if (total == 10000) {
		fmt.Println(time.Now().Sub(t0))
	}
	c &lt;- 1
	return 0;
}

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

答案1

得分: 2

根据错误提示,你的代码存在语法错误。
更具体地说,你在第36行忘记了加逗号:

...
func httpGet ()  int {
    tr := &amp;http.Transport{
        Dial: make_dialer(false), // &lt;-- 逗号
    }
...
英文:

As error says, you have made syntax error.
More specifically, you have forgotten to put comma on line 36:

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

huangapple
  • 本文由 发表于 2017年5月9日 13:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/43861546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定