Matching

Examples Filter
case blocks in go automatically break after their branch is executed and the user must use the "fallthrough" keyword to have it continue to match other case blocks.
package main

import (
    "fmt"
    "time"
)

func main() {

    i := 2
    fmt.Print("Write ", i, " as ")
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    }

    switch time.Now().Weekday() {
    case time.Saturday, time.Sunday:
        fmt.Println("It's the weekend")
    default:
        fmt.Println("It's a weekday")
    }

    t := time.Now()
    switch {
    case t.Hour() < 12:
        fmt.Println("It's before noon")
    default:
        fmt.Println("It's after noon")
    }

    whatAmI := func(i interface{}) {
        switch t := i.(type) {
        case bool:
            fmt.Println("I'm a bool")
        case int:
            fmt.Println("I'm an int")
        default:
            fmt.Printf("Don't know type %T\n", t)
        }
    }
    whatAmI(true)
    whatAmI(1)
    whatAmI("hey")
}
// A match statement is a shorter way to write a sequence of if - else statements.
// When a matching branch is found, the following statement block will be run, and the final expression will be returned.
// The else branch will be evaluated when no other branches match.
os := 'windows'
print('V is running on ')
match os {
	'darwin' { println('macOS.') }
	'linux'  { println('Linux.') }
	else     { println(os) }
}

number := 1
s := match number {
	1    { 'one' }
	2    { 'two' }
	else { 
		println('this works too')
		'many' 
	}
}

// A match statement can also be used to branch on the variants of an enum by using the shorthand .variant_here syntax.
enum Color {
	red
	blue
	green
}
fn is_red_or_blue(c Color) bool {
	return match c {
		.red { true }
		.blue { true }
		else { false }
	}
}
Last Run  :
================ V panic ================
   module: builtin
 function: get()
     file: /home/ubuntu/code_runner/root_volume/v/vlib/builtin/array.v
     line: 153
  message: array.get: index out of range (i == -1, a.len == 0)
=========================================
                                               | 0x56059535eb5a | /code/root/v/v(panic_debug+0x131) 
                                               | 0x56059535d740 | /code/root/v/v(array_get+0xaf) 
                                               | 0x5605953a71c3 | /code/root/v/v(compiler__CGen_insert_before+0x60) 
                                               | 0x5605953cc01b | /code/root/v/v(compiler__Parser_match_statement+0x1c4) 
                                               | 0x56059539477c | /code/root/v/v(compiler__Parser_statement+0x458) 
                                               | 0x56059538fde4 | /code/root/v/v(compiler__Parser_parse+0xe69) 
                                               | 0x5605953d0089 | /code/root/v/v(compiler__V_parse+0x15b) 
                                               | 0x5605953d0306 | /code/root/v/v(compiler__V_compile+0x261) 
                                               | 0x5605953f4eb6 | /code/root/v/v(main__main+0x649) 
                                               | 0x5605953f788b | /code/root/v/v(main+0x68) 
                                               | 0x7fee2618cb97 | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) 
                                               | 0x56059535ceea | /code/root/v/v(_start+0x2a)