在不同函数中定义的标志和同步给出错误

发布于 2025-01-24 22:13:47 字数 2010 浏览 2 评论 0原文

我想与某些标志同时运行两个函数,但是如果他们不调用标志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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2025-01-31 22:13:47

您不应将标志arg语句分别放置,将它们放入一个函数中,一次调用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")
}

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")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文