Newbie Question On fmt.PrintLn

Hello - total noob question. Why do I only get the “Failed to ping…” message written to the console when running the below?

func main() {
	url := "abc.com"
	timeout := time.Second * 3
	pingTicker := time.NewTicker(30 * time.Second)
	for {
		select {
			case <-pingTicker.C:
				success := ping(url, timeout)
				if !success {
					pingTicker.Stop()
					fmt.Println("Failed to ping...")
					return
				}
			}
	}
	fmt.Println("Process terminated")
}

Hello. Note that after the process executes the line you mentioned (fmt.Println("Failed to ping...")), it returns from the function (the next line is return), so it means the main() function ends — so nothing else will be executed.

Yep, and if you need scape from the “for” you need this, otherwise you will get an error, or in some cases, you will wait forever “for”

Failed to ping…
fatal error: all goroutines are asleep - deadlock!

package main

import (
	"fmt"
	"time"
)

func main() {
	url := "abc.com"
	timeout := time.Second * 3
	pingTicker := time.NewTicker(1 * time.Second)

loop:
	for {
		select {
			case <-pingTicker.C:
				success := ping(url, timeout)
				if !success {
					pingTicker.Stop()
					fmt.Println("Failed to ping...")
					break loop
				}
			}
	}

	fmt.Println("Process terminated")
}

func ping(u string, t time.Duration) bool {
  return false
}

Thank you both! I feel stupid - for some reason I thought the return statement in the for loop had a different meaning.