Placeholders


package main
import "fmt"

func main() {
    var first_name string
    var last_name string
    var grade int
    var student_id int
    var login string
    var gpa float64

    fmt.Println("Please enter the following information:")
    fmt.Print("First name: ")
    fmt.Scanf("%s", &first_name)
    fmt.Print("Last name: ")
    fmt.Scanf("%s", &last_name)
    fmt.Print("Grade (9 - 12): ")
    fmt.Scanf("%d", &grade)
    fmt.Print("Student ID: ")
    fmt.Scanf("%d", &student_id)
    fmt.Print("Log-in: ")
    fmt.Scanf("%s", &login)
    fmt.Print("GPA (0.0 - 4.0): ")
    fmt.Scanf("%f", &gpa)

    fmt.Println("Your information:")
    fmt.Printf("\tLogin: %s\n", login)
    fmt.Printf("\tID: %s\n", student_id)
    fmt.Printf("\tName: %s %s\n", first_name, last_name)
    fmt.Printf("\tGPA: %f\n", gpa)
    fmt.Printf("\tGrade: %d\n", grade)
}




...But wait! There's more...

package main
import "fmt"

// one verison all in main...
// another version uses a different func

func main() {
    var name string
    var age int
    var age_back int
    var age_plus int

    fmt.Println("Hello. What is your name?")
    fmt.Scanf("%s", &name)
    fmt.Printf("Hi %s! How old are you? ", name)
    fmt.Scanf("%d", &age)

    age_back = age - 5
    fmt.Printf("Did you know that five years ago, you were be %d years old?\n", age_back)
    age_plus = age + 5
    fmt.Printf("...and that five years from now, you will be %d years old. Imagine that!\n", age_plus)
}

* This was how I solved one exercise from Graham Mitchell's Programming By Doing website.

strings.Repeat





Source: http://golang.org/src/pkg/strings/strings.go?s=9058:9097#L355

printf and println



println

package main
import "fmt"

func main() {
  name := "jim"
  fmt.Println("Hello world %s"\n, name)
}

go run a.go will give us Hello world %s jim

printf

package main
import "fmt"

func main() {
  name := "jim"
  fmt.Printf("Hello world %s\n", name)
}

This time, we get Hello world jim

using %s and %d



package main
import "fmt"

func main() {
  my_name := "JimS"
  my_age := 34 
  my_height := 74
  my_weight := 180 
  my_eyes := "brown"
  my_hair := "black"

  fmt.Printf("My name is %s.\n", my_name)
  fmt.Printf("I'm about %d inches tall.\n", my_height)
  fmt.Printf("I used to be %d pounds heavy.\n", my_weight)
  fmt.Printf("I have %s eyes and %s hair.\n", my_eyes, my_hair)
  fmt.Printf("adding these %d, %d, %d equals %d\n", 
    my_age, my_height,my_weight, my_age + my_height + my_weight)
}

convert float to int



package main
import "fmt"

func main() {
    this := 20
    that := 30.2342

    fmt.Println("There =", this - int(that))
}