1 回答
TA贡献1869条经验 获得超4个赞
您不应该将标志 arg 语句分开放置,将它们放入单个 func 中,调用flag.Parse()一次,然后将参数传递给pingone()和pingtwo()。
我在下面调整了您的代码,它应该可以解决您的问题
package main
import (
"flag"
"fmt"
"net/http"
"sync"
"time"
)
func pingone(websiteone *string) {
fmt.Println("website:", *websiteone)
eurl := "https://thesiteone.com/"
happ := "/subpage"
for {
resp, err := http.Get(eurl + *websiteone + happ)
if err != nil {
continue
}
fmt.Println(resp)
time.Sleep(2 * time.Second)
}
}
func pingtwo(websitetwo *string) {
fmt.Println("website:", *websitetwo)
eurltwo := "https://thesitetwo.com/"
happtwo := "/subpage"
for {
resp, err := http.Get(eurltwo + *websitetwo + happtwo)
if err != nil {
continue
}
fmt.Println(resp)
time.Sleep(2 * time.Second)
}
}
func main() {
websiteone := flag.String("websiteone", "adminone", "Zdefault website")
websitetwo := flag.String("websitetwo", "admintwo", "Zdefault website")
flag.Parse()
var wg sync.WaitGroup
fmt.Printf("Both pingone and pingtwo started\n")
wg.Add(2)
go pingone(websiteone)
go pingtwo(websitetwo)
wg.Wait()
fmt.Printf("both pingone and pingtwo have finished\n")
}
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报