Basic Syntax and Data Types

Learn the fundamental syntax of Go, including variable declaration, basic data types (integers, floats, strings, booleans), and operators.


Go Language Basics

Introduction

This document introduces the fundamental syntax and data types of the Go programming language.

Basic Syntax

Go's syntax is relatively clean and easy to learn. Here are some key aspects:

Package Declaration

Every Go program must start with a package declaration. The main package is special; it's where the program execution begins.

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
} 

Imports

The import keyword is used to include packages containing functions and types you want to use in your program. The fmt package, for example, provides formatted input and output.

Functions

Functions are defined using the func keyword. The main function is the entry point of the program.

Statements and Semicolons

Go generally infers semicolons at the end of lines, so you don't usually need to include them explicitly. However, they are required in certain situations, like when writing multiple statements on a single line.

Comments

Go supports both single-line comments (//) and multi-line comments (/* ... */).

Data Types

Go has a variety of built-in data types:

Integers

Represent whole numbers. Go provides several integer types with different sizes (and therefore different ranges):

  • int: A signed integer type. The size is platform-dependent (typically 32-bit or 64-bit).
  • int8, int16, int32, int64: Signed integers with specific sizes (8, 16, 32, and 64 bits, respectively).
  • uint: An unsigned integer type. The size is platform-dependent (typically 32-bit or 64-bit).
  • uint8, uint16, uint32, uint64: Unsigned integers with specific sizes.
  • byte: An alias for uint8. Commonly used for representing byte data.
  • rune: An alias for int32. Represents a Unicode code point.
var age int = 30
var distance uint = 1000
var smallNumber int8 = 127 

Floating-Point Numbers

Represent numbers with fractional parts.

  • float32: Single-precision floating-point number.
  • float64: Double-precision floating-point number.
var pi float64 = 3.14159
var temperature float32 = 25.5 

Strings

Represent sequences of characters. Strings are immutable in Go.

var name string = "John Doe"
var message string = `This is a
multi-line string.` 

Booleans

Represent truth values (true or false).

var isAdult bool = true
var isLoggedIn bool = false 

Variable Declaration

Variables can be declared using the var keyword, followed by the variable name, type, and optionally an initial value.

var age int
age = 30

var name string = "Alice"

// Type inference using := (short variable declaration)
count := 10 // count is inferred to be an int

// Multiple variable declaration
var (
    firstName string = "Bob"
    lastName  string = "Smith"
)

// Zero values
var number int      // number is initialized to 0
var text   string   // text is initialized to "" (empty string)
var flag   bool     // flag is initialized to false 

Operators

Go supports a variety of operators for performing arithmetic, comparison, logical, and bitwise operations.

Arithmetic Operators

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulo - remainder after division)
a := 10
b := 5
sum := a + b        // 15
difference := a - b // 5
product := a * b     // 50
quotient := a / b    // 2
remainder := a % b   // 0 

Comparison Operators

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)
x := 5
y := 10
equal := x == y      // false
notEqual := x != y   // true
greaterThan := y > x  // true
lessThan := x < y     // true 

Logical Operators

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)
p := true
q := false
andResult := p && q  // false
orResult := p || q   // true
notP := !p          // false 

Assignment Operators

  • = (Assignment)
  • += (Add and assign)
  • -= (Subtract and assign)
  • *= (Multiply and assign)
  • /= (Divide and assign)
  • %= (Modulo and assign)
count := 5
count += 3 // count is now 8
count -= 2 // count is now 6