statement in go

In Go, a statement is a single instruction which performs a specific operation. A statement is typically terminated by a semicolon (;), but it is not required in cases where a newline character is used instead. Here are some examples of basic statements in Go:

main.go
x := 5; // variable declaration and assignment statement
y := "hello, world!"; // variable declaration and assignment statement
fmt.Println(x, y); // function call statement

if x > 3 {
    fmt.Println("x is greater than 3!"); // if statement with a function call statement as the body
}

for i := 0; i < 5; i++ {
    fmt.Println(i);
} // for loop statement with a function call statement as the body

switch x {
    case 1:
        fmt.Println("x is 1!");
    case 2:
        fmt.Println("x is 2!");
    default:
        fmt.Println("x is something else!"); // switch statement with multiple function call statements as the body
}
632 chars
21 lines

related categories

gistlibby LogSnag