在不同函数中定义的标志和同步给出错误
我想与某些标志同时运行两个函数,但是如果他们不调用标志go run ping.go
ie。,他们将使用默认值,则以下Golang脚本正在工作。
ping.go文件下面
package main
import (
// "io/ioutil"
// "log"
"flag"
"fmt"
"net/http"
"sync"
"time"
)
func pingone() {
websiteone := flag.String("websiteone", "adminone", "Zdefault website")
flag.Parse()
// using/printing flags to avoid error
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 := flag.String("websitetwo", "admintwo", "Zdefault website")
flag.Parse()
// using/printing flags to avoid error
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() {
var wg sync.WaitGroup
fmt.Printf("Both pingone and pingtwo started\n")
wg.Add(2)
go pingone()
go pingtwo()
wg.Wait()
fmt.Printf("both pingone and pingtwo have finished\n")
}
,但如果我们同时调用两个标志 go run ping.go -websitetwo ='secondsuburl' - websiteone ='firstSuburl'
结果,他们
对上述代码被破坏了**
脚本将运行两个函数都使用Sync同时启动不同的网站。
不要感到困惑,这里将URL拆分为三个部分/字符串:第二部分是标志定义的位置。
下面的输出
root@localhost:~# go run ping.go --websiteone=admin1 --websitetwo=admin2
Both pingone and pingtwo started
flag provided but not defined: -websiteone
Usage of /tmp/go-build010683275/b001/exe/ping:
-websiteone string
Zdefault website (default "adminone")
website: admin1
-websitetwo string
Zdefault website (default "admintwo")
exit status 2
I want to run two functions simultaneously with some flags, but the below golang script is working if they are't calling the flags go run ping.go
ie., they will use default values.
ping.go file below
package main
import (
// "io/ioutil"
// "log"
"flag"
"fmt"
"net/http"
"sync"
"time"
)
func pingone() {
websiteone := flag.String("websiteone", "adminone", "Zdefault website")
flag.Parse()
// using/printing flags to avoid error
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 := flag.String("websitetwo", "admintwo", "Zdefault website")
flag.Parse()
// using/printing flags to avoid error
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() {
var wg sync.WaitGroup
fmt.Printf("Both pingone and pingtwo started\n")
wg.Add(2)
go pingone()
go pingtwo()
wg.Wait()
fmt.Printf("both pingone and pingtwo have finished\n")
}
but if we calls these both flags simultaneouslygo run ping.go --websitetwo='secondsuburl' --websiteone='firstsuburl'
results they get wrecked
** About the above code **
The script will run two functions both are pinging different website simultaneously using sync.
Don't get confused, here the url is splitted into three parts/string : the second part is where the flag defined.
Output below
root@localhost:~# go run ping.go --websiteone=admin1 --websitetwo=admin2
Both pingone and pingtwo started
flag provided but not defined: -websiteone
Usage of /tmp/go-build010683275/b001/exe/ping:
-websiteone string
Zdefault website (default "adminone")
website: admin1
-websitetwo string
Zdefault website (default "admintwo")
exit status 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应将标志arg语句分别放置,将它们放入一个函数中,一次调用
flag.parse()
一次,然后将参数传递到两个pingone()
和pingtwo()
。我在下面调整了您的代码,它应该解决您的问题
You should not put the flag arg statements separately, place them into a single func, call the
flag.Parse()
once, then pass the arguments into bothpingone()
andpingtwo()
.I adjusted your code below, it should fix your issue