Flags defined in different functions with sync giving error

Issue

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

Solution

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

Answered By – novalagung

Answer Checked By – David Marino (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.