Programming Fundamentals/Variables/Go: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
Creating
(No difference)

Revision as of 00:36, 21 November 2016

hello.go

package main

import "fmt"

func main() {
    fmt.Printf("Variables and Data Types\n")
    var i int = 1234567890
    var f32 float32 = 1.2345678
    var f64 float64 = 1.234567890123456
    var s string = "string"
    
    fmt.Printf("int: %d\n", i)
    fmt.Printf("float32: %.7f\n", f32)
    fmt.Printf("float64: %.15f\n", f64)
    fmt.Printf("string: %s\n\n", s)
    
    fmt.Printf("Constants\n")
    const pi float32 = 3.14159
    const c string = "Constant"
    fmt.Printf("%f\t%s\n\n", pi, c)

    fmt.Printf("Arithmetic Operators\n")
    var a int = 3
    var b int = 2
    fmt.Printf("a = %d\n", a)
    fmt.Printf("b = %d\n", b)
    fmt.Printf("a + b = %d\n", a + b)
    fmt.Printf("a - b = %d\n", a - b)
    fmt.Printf("a * b = %d\n", a * b)
    fmt.Printf("a / b = %d\n\n", a / b)

    fmt.Printf("Assignment Operators\n")
    a += b
    fmt.Printf("a += b is %d\n", a)
    a -= b
    fmt.Printf("a -= b is %d\n", a)
    a *= b
    fmt.Printf("a *= b is %d\n", a)
    a /= b
    fmt.Printf("a /= b is %d\n\n", a)

    fmt.Printf("User Input\n")
    var name string
    fmt.Printf("What is your name? ")
    fmt.Scanln(&name)
    fmt.Printf("Hello %s!\n", name)
}

Try It

Copy and paste the code above into one of the following free online development environments or use your own Go compiler / interpreter / IDE.

See Also