-4
我得到了一個用Golang寫的腳本,我不太明白。我想知道他爲什麼在腳本里面寫了go server.Start()
?爲什麼不乾脆寫server.Start?
Golang:在腳本內部執行命令?
package main
import (
"github.com/miekg/dns"
"testing"
"time"
)
const TEST_ADDR = "127.0.0.1:9953"
func TestDNSResponse(t *testing.T) {
server := NewDNSServer(&Config{
dnsAddr: TEST_ADDR,
})
go server.Start()
// Allow some time for server to start
time.Sleep(150 * time.Millisecond)
m := new(dns.Msg)
m.Id = dns.Id()
m.Question = []dns.Question{
dns.Question{"docker.", dns.TypeA, dns.ClassINET},
}
c := new(dns.Client)
_, _, err := c.Exchange(m, TEST_ADDR)
if err != nil {
t.Error("Error response from the server", err)
}
server.Stop()
c = new(dns.Client)
_, _, err = c.Exchange(m, TEST_ADDR)
if err == nil {
t.Error("Server still running but should be shut down.")
}
}