BudiBadu Logo

Samplebadu

Code with Example
BudiBadu Logo
Samplebadu

Go by Example: Values

Go 1.23

Understanding Go's basic value types is crucial for writing robust applications. This code example explores strings, integers, floats, and booleans, demonstrating how to work with literals, perform arithmetic and logical operations, and understand the behavior of Go's strongly-typed system.

Code

package main

import "fmt"

func main() {
    // Strings can be concatenated with +
    fmt.Println("go" + "lang")

    // Integers and floats support arithmetic
    fmt.Println("1+1 =", 1+1)
    fmt.Println("7.0/3.0 =", 7.0/3.0)

    // Booleans with logical operators
    fmt.Println(true && false)
    fmt.Println(true || false)
    fmt.Println(!true)
}

Explanation

Go provides a rich set of basic value types including booleans (bool), strings, and various numeric types (integers and floating-point numbers). These fundamental types are the building blocks for all Go programs and understanding their behavior is crucial for writing robust applications.

Strings in Go are immutable sequences of bytes, UTF-8 encoded by default. String concatenation using the + operator creates a new string rather than modifying either original string. The len() function returns the byte length, not character count—a crucial distinction for Unicode text where characters can span multiple bytes.

Numeric types include both signed integers (int8, int16, int32, int64, int) and unsigned integers (uint8, uint16, uint32, uint64, uint). Floating-point types are float32 (single-precision, ~6-7 decimal digits) and float64 (double-precision, ~15 decimal digits, the default). Integer division truncates the decimal part (7/3 = 2), while float division preserves it (7.0/3.0 ≈ 2.333).

Boolean logical operators use short-circuit evaluation for both efficiency and safety: with AND (&&), if the first operand is false, the second isn't evaluated; with OR (||), if the first is true, the second isn't evaluated. This prevents unnecessary computation and potential runtime errors.

Important characteristics of Go's type system:

  • No implicit type conversions—prevents subtle bugs at compile time
  • Zero values for uninitialized variables (0 for numbers, false for bools, "" for strings)
  • Type inference available with := operator
  • Strict typing catches errors early in development

Code Breakdown

7
String concatenation using the + operator. This combines "go" and "lang" into "golang". Because strings are immutable in Go, this operation creates a new string rather than modifying either original.
10-11
These lines demonstrate the difference between integer and floating-point arithmetic. Line 10 shows integer addition (1+1=2). Line 11 shows floating-point division: 7.0/3.0 gives approximately 2.333... If you used integers (7/3), you would get 2 because integer division discards the remainder.
14-16
Boolean logic operations. Line 14: true AND false = false (both must be true). Line 15: true OR false = true (at least one must be true). Line 16: NOT true = false (inverts the value). Go uses short-circuit evaluation for efficiency and safety.