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

Flags defined in different functions with sync giving error

我想 运行 两个函数同时使用一些标志,但如果它们不调用标志,下面的 golang 脚本就可以工作 go run ping.go 即,它们将使用默认值。

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' 结果他们失事了

** 关于上面的代码**

该脚本将 运行 两个函数都使用同步同时 ping 不同的网站。

不要混淆,这里 url 被分成三个 parts/string :第二部分是标志定义的地方。

下面的输出

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

你不应该单独放置标志 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")
}