Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to use switch statement in Go

4 Answers

0 votes
package main
 
import "fmt"
 
func main() {
	n := 3
    switch n {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    case 4:
        fmt.Println("four")
    }
}
 
 
 
/*
run:
 
three

*/

 



answered Feb 21, 2020 by avibootz
0 votes
package main
 
import (
    "fmt"
    "time"
)
 
func main() {
	t := time.Now()
	
    switch {
    case t.Hour() > 13:
        fmt.Println("> 13")
    default:
        fmt.Println("< 13")
    }
}
 
 
 
/*
run:
 
> 13

*/

 



answered Feb 21, 2020 by avibootz
0 votes
package main
 
import (
    "fmt"
)
 
func main() {
	f := func(i interface{}) {
        switch t := i.(type) {
        case bool:
            fmt.Println("bool")
        case int:
            fmt.Println("int")
		case string:
            fmt.Println("string")
        default:
            fmt.Printf("type is = ", t)
        }
    }
    f(12)
    f(true)
    f("golang")
    f(false)
	f(3.14)
}
 
 
 
/*
run:
 
int
bool
string
bool
type is = %!(EXTRA float64=3.14)

*/

 



answered Feb 21, 2020 by avibootz
0 votes
package main

import (
    "fmt"
)

func main() {
	switch os := "go"; os {
	case "python":
		fmt.Println("Python")
	case "go":
		fmt.Println("Go")
	default:
		fmt.Printf("default")
	}
}



/*
run:

Go

*/

 



answered Mar 6, 2020 by avibootz
...