1

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
3
  • 2
    I think you should only be calling flag.Parse once after defining both of your flags. You're calling it twice.
    – byxor
    Apr 27, 2022 at 14:01
  • 1
    You forget to pass the waitgroup to each function to call Done when it finishes. This code will hang forever Apr 27, 2022 at 19:39
  • 1
    I think you should call the options with one dash instead two Apr 27, 2022 at 19:40

1 Answer 1

1

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")
}
4
  • 2
    You forget to ALSO pass the waitgroup to each function to call Done when it finishes. This code will hang forever Apr 27, 2022 at 19:39
  • 1
    even with/without calling the .Done(), the code will run forever. the for loop within each goroutine is infinite loop. I don't know why the code designed that way. but my answer here only focus on the flag parsing part
    – novalagung
    Apr 27, 2022 at 19:52
  • 1
    Sure, i did no see it was on the original code too Apr 27, 2022 at 21:01
  • Actually the point of my question is running two functions with multiple flags. Thanks that worked. @novalagung. and here i just recreated a similar example or issue that i faced in my another script (that script is too long with many functions , so its hard to explain here )
    – Anas S
    Apr 28, 2022 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.