DEV Community

Kervie Sazon
Kervie Sazon

Posted on • Edited on

Go Learning Notes - Part 1: Variables, Constant, Data Types & Pointers)

VS Code

VS Code (Visual Studio Code) is a lightweight and powerful code editor used for writing and managing code.

What I Learned:

  • How to create a new Go file (main.go)
  • Installing Go extensions
  • Running Go programs using the terminal:
go run main.go
Enter fullscreen mode Exit fullscreen mode

package

In Go, every file belongs to a package.

package main
Enter fullscreen mode Exit fullscreen mode

package main is required for executable programs.

It tells Go that this file will run as an application.

import

The import keyword allows us to use packages from Go's standard library.

import "fmt"
Enter fullscreen mode Exit fullscreen mode

"fmt" is used for formatted input and output.

Without importing it, we cannot use functions like fmt.Println() or fmt.Scan().

func main ()

The main function is the entry point of a Go program.

func main() {
    fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

The program starts executing inside func main().

var (Variables)

Variables store data.

var name string = "Kervie"
var age int = 23
Enter fullscreen mode Exit fullscreen mode

Can also shortened by : before the =;

name := "Kervie"
age := 23
Enter fullscreen mode Exit fullscreen mode

const (Constant)

Constant store values that Cannot be changed.

const pi = 3.14
Enter fullscreen mode Exit fullscreen mode

Once declared, it cannot be reassigned.

fmt.Println()

Used to print output to the console.

fmt.Println("Hello!")
Enter fullscreen mode Exit fullscreen mode

Automatically adds a newline(ln) at the end.

%v (Format Verb)

%v is used inside formatted printing functions.

fmt.Printf("My name is %v", name)
Enter fullscreen mode Exit fullscreen mode

%v prints the value in its default format.

Other examples:

  • %d - integer
  • %s - string
  • %f - float

Pointers

var x int = 10
var p *int = &x
Enter fullscreen mode Exit fullscreen mode

&x - gets the address of x
*p - gets the value stored at that address
Example:

fmt.Println(x)   // 10
fmt.Println(p)   // memory address
fmt.Println(*p)  // 10
Enter fullscreen mode Exit fullscreen mode

User Input

Go allows user input using the fmt package.

fmt.Scan()

Used to read input from the user.

var name string
fmt.Scan(&name)
Enter fullscreen mode Exit fullscreen mode

The & is important because fmt.Scan() needs the memory address of the variable.

It stores the user input inside the variable.

Example:

package main

import "fmt"

func main() {
    var name string

    fmt.Println("Enter your name:")
    fmt.Scan(&name)
    fmt.Println("Hello,", name)
}
Enter fullscreen mode Exit fullscreen mode

Example For Better Understanding

A simple Go Conference Booking App.

Program Stucture

package main

package main
Enter fullscreen mode Exit fullscreen mode

import "fmt"

import "fmt"
Enter fullscreen mode Exit fullscreen mode

func main()

func main () {

}
Enter fullscreen mode Exit fullscreen mode

Variables (var)

var conferenceName = "Go Conference"
var remainingTickets = 50
Enter fullscreen mode Exit fullscreen mode
  • var is used to declare variables.
  • Variables can change during program execution.
  • Go automatically detects the type if not specified.

Example with explicit type:

var fName string
var lName string
var email string
var userTickets int
Enter fullscreen mode Exit fullscreen mode

Constants (const)

const conferenceTickets = 50
Enter fullscreen mode Exit fullscreen mode
  • const is used for values that never change.
  • Once declared, it cannot be modified.

Printing Output

fmt.Printf()

Used for formatted output.

fmt.Printf("conferenceName is %T\n", conferenceName)
Enter fullscreen mode Exit fullscreen mode

Format verbs learned:

Verb Meaning
%T Type of variable
%v Default value format
\n New line

Example:

fmt.Printf("Welcome to our %v booking application!\n", conferenceName)
Enter fullscreen mode Exit fullscreen mode

fmt.println()

fmt.Println("Get your tickets here to attend")

Enter fullscreen mode Exit fullscreen mode
  • Prints text
  • Automatically adds a new line

fmt.Print()

fmt.Print("Enter your first name: ")
Enter fullscreen mode Exit fullscreen mode
  • Prints text
  • Does NOT automatically add a new line

User Input

fmt.Scan()

fmt.Scan(&fName)
Enter fullscreen mode Exit fullscreen mode
  • The &symbol passes the memory address of the variable.
  • fmt.Scan() stores the user input into that memory location.

Example:

var fName string
fmt.Scan(&fName)
Enter fullscreen mode Exit fullscreen mode

Pointers (Related to &)

When using:

fmt.Scan(&fName)
Enter fullscreen mode Exit fullscreen mode
  • &fName means: "give me the address of fName"
  • This allows the function to modify the variable.

Updating Variables

remainingTickets = remainingTickets - userTickets
Enter fullscreen mode Exit fullscreen mode
  • This updates the value stored in remainingTickets.
  • Since it is declared with var, it can change.

Full Example Logic Simplified

remainingTickets = remainingTickets - userTickets

fmt.Printf("Thank you %v %v for booking %v tickets.\n", fName, lName, userTickets)
fmt.Printf("%v remaining tickets for %v", remainingTickets, conferenceName)
Enter fullscreen mode Exit fullscreen mode

Full source code for better understanding:

package main

import "fmt"

func main() {
    var conferenceName = "Go Conference"
    const conferenceTickets = 50
    var remainingTickets = 50

    fmt.Printf("conferenceName is %T, conferenceTickets is %T, remainingTickets is %T\n", conferenceName, conferenceTickets, remainingTickets)

    fmt.Printf("Welcome to our %v booking application!\n", conferenceName)
    fmt.Printf("We have %v tickets and %v are vailable\n", conferenceTickets, remainingTickets)
    fmt.Println("Get your tickets here to attend")

    var fName string
    var lName string
    var email string
    var userTickets int
    // asking for user details
    fmt.Print("Enter you first name: ")
    fmt.Scan(&fName)

    fmt.Print("Enter you lasr name: ")
    fmt.Scan(&lName)

    fmt.Print("Enter you email address: ")
    fmt.Scan(&email)

    fmt.Print("How many ticket you get: ")
    fmt.Scan(&userTickets)

    remainingTickets = remainingTickets - userTickets

    fmt.Printf("Thank you %v %v for booking %v tickets. You will recieve a confirmation email at %v\n", fName, lName, userTickets, email)
    fmt.Printf("%v remaining tikcets for %v", remainingTickets, conferenceName)

}

Enter fullscreen mode Exit fullscreen mode

Today I learned how to structure a Go program using package main, import, and func main() as the entry point. I practiced declaring variables with var, creating constants with const, and understanding data types using format verbs like %T and %v. I used fmt.Print, fmt.Println and fmt.Printf to display formatted output in the console. I also learned how to collect user input with fmt.Scan() and why pointers (&) are needed to store values in variables.

Top comments (0)