在不同函数中定义的标志,使用同步时出现错误。

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

Flags defined in different functions with sync giving error

问题

我想同时运行两个带有一些标志的函数,但是下面的 Golang 脚本只有在它们没有调用标志 go run ping.go 时才能正常工作,即它们将使用默认值。

以下是 ping.go 文件的内容:

package main

import (
	"flag"
	"fmt"
	"net/http"
	"sync"
	"time"
)

func pingone() {
	websiteone := flag.String("websiteone", "adminone", "默认网站")

	flag.Parse()

	fmt.Println("网站:", *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", "默认网站")

	flag.Parse()

	fmt.Println("网站:", *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("pingone 和 pingtwo 都已启动\n")

	wg.Add(2)
	go pingone()
	go pingtwo()

	wg.Wait()
	fmt.Printf("pingone 和 pingtwo 都已完成\n")

}

但是,如果同时调用这两个标志,结果会出错:

go run ping.go --websitetwo='secondsuburl' --websiteone='firstsuburl'

关于上述代码

该脚本将同时运行两个函数,它们都使用 sync 包来同时对不同的网站进行 ping 操作。

不要混淆,这里的 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
默认网站 (default "adminone")
website: admin1
-websitetwo string
默认网站 (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 simultaneously
go 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

答案1

得分: 1

你不应该将标志参数语句分开放置,而是将它们放入一个单独的函数中,在调用flag.Parse()一次后,将参数传递给pingone()pingtwo()

我调整了你的代码如下,应该可以解决你的问题:

package main

import (
	"flag"
	"fmt"
	"net/http"
	"sync"
	"time"
)

func pingone(websiteone *string) {
	fmt.Println("网站:", *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("网站:", *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", "默认网站")
	websitetwo := flag.String("websitetwo", "admintwo", "默认网站")

	flag.Parse()

	var wg sync.WaitGroup

	fmt.Printf("pingone和pingtwo都已启动\n")

	wg.Add(2)
	go pingone(websiteone)
	go pingtwo(websitetwo)

	wg.Wait()
	fmt.Printf("pingone和pingtwo都已完成\n")
}
英文:

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 both pingone() and pingtwo().

I adjusted your code below, it should fix your issue

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

huangapple
  • 本文由 发表于 2022年4月27日 21:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/72029314.html
匿名

发表评论

匿名网友

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

确定