Day 3: Print Formatted Data and DataTypes in GoLang

  • It takes a template string that contains the text that needs to be formatted.

Printf - "Print Formatter" this function allows you to format numbers, variables and strings into the first string parameter you give it.

Print - "Print" This cannot format anything, it simply takes a string and print it.

Println - "Print Line" same thing as Printf() however it will append a newline character\n.

**Printing The verbs:

General:**

%v    the value in a default format
    when printing structs, the plus flag (%+v) adds field names
%#v    a Go-syntax representation of the value
%T    a Go-syntax representation of the type of the value
%%    a literal percent sign; consumes no value

Sample Code

package main

import "fmt"

func main() {
    var conferenceName = "Go Conference"
    const conferenceTickets = 50
    var remainingTickets = 50
    fmt.Printf("Welcome to %v booking application\n", conferenceName) // %v the value in a default format when printing structs, the plus flag (%+v) adds field names
    fmt.Printf("we have total of %v  tickets and %v are still Available\n", conferenceTickets, remainingTickets)
    fmt.Println("Get your tickets to attend")
}

Formatting the packages with other verbs

DataTypes in Go

  • Strings
  • Integers
  • Maps
  • Booleans
  • Arrays

  • In any programming languages you have multiple data types.

Strings:

  • For textual data, defined with double quotes, eg: "This is string"

Integers:

  • Representing whole numbers, Positive and negative, eg: 5, -120, 100

  • There are many more numeric data types.

Sample Code

package main

import "fmt"

func main() {
    var conferenceName = "Go Conference"
    const conferenceTickets = 50
    var remainingTickets = 50
    fmt.Printf("Welcome to %v booking application\n", conferenceName) // %v the value in a default format when printing structs, the plus flag (%+v) adds field names
    fmt.Printf("we have total of %v  tickets and %v are still Available\n", conferenceTickets, remainingTickets)
    fmt.Println("Get your tickets to attend")

    // DataTypes in Go
    var userName string
    var userTickets int

    userName = "sandeep"
    userTickets = 5

    fmt.Printf("User %v booked the %v  tickets for a movie.\n", userName, userTickets)
}

What is Pointer?

Pointers in Go programming language or Golang is a variable that is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always found in hexadecimal format(starting with 0x like 0xFFAAF etc.).

Sample Code:

package main

import "fmt"

func main() {
    conferenceName := "Go Conference"
    const conferenceTickets int = 50
    var remainingTickets uint = 50
    fmt.Printf("Welcome to %v booking application\n", conferenceName) // %v the value in a default format when printing structs, the plus flag (%+v) adds field names
    fmt.Printf("we have total of %v  tickets and %v are still Available\n", conferenceTickets, remainingTickets)
    fmt.Println("Get your tickets to attend")

    // UserInput in Go
    var firstName string
    var lastName string
    var email string
    var userTickets int
    fmt.Println("Enter your first Name: ")
    fmt.Scan(&firstName)

    fmt.Println("Enter your Last Name: ")
    fmt.Scan(&lastName)

    fmt.Println("Enter your email: ")
    fmt.Scan(&email)

    fmt.Println("How many tickets required: ")
    fmt.Scan(&userTickets)

    fmt.Printf("Thank you %v %v for booking %v tickets. You will recieve a confirmation email at %v\n", firstName, lastName, userTickets, email)
}

Note: Difference In datatypes for "int" and "uint" - uint means “unsigned integer” while int means “signed integer”. Unsigned integers only contain positive numbers (or zero).