Master Go Basics: Packages, Types, Variables, Constants, and Functions

This tutorial introduces Go's core concepts—including package structure, built‑in types, variable and constant declarations, and function definitions—explaining syntax nuances, import styles, type creation, and initialization patterns to help beginners quickly become productive in Go.

ITPUB
ITPUB
ITPUB
Master Go Basics: Packages, Types, Variables, Constants, and Functions

Package Structure

Go programs are organized into packages. Execution starts from the main package.

package main

Import Styles

Single import

import "os"
import "fmt" // comment style same as C

Grouped import

import (
    "fmt"
    "os"
)

Aliased import

import f "fmt"
f.Println("go go go")

Identifiers beginning with an uppercase letter are exported (public) and can be accessed from other packages; lowercase identifiers are private.

Basic Types

Built‑in types include:

int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex128 complex64
bool byte rune string error

Commonly used types are int (platform‑dependent size), string, and bool ( true / false).

Defining New Types

Use type NewName UnderlyingType to create a distinct type, similar to C++ typedef. The new type is not interchangeable with its underlying type, providing strong type safety.

type ProgramType string
var t1 ProgramType = "Golang"
var t2 ProgramType = "C++"

Type Conversion

Conversions are explicit: T(v) converts value v to type T.

var i int = 66
var f float32 = float32(i)
fmt.Printf("%T %v %T %v
", i, i, f, f)

Variables

Declaration

var imVar int                     // single
var imVar1, imVar2, imVar3 int   // multiple of same type
var (
    i int
    b bool
    s string
)

Initialization

Zero value: uninitialized variables receive default zero values (0, false, "").

Explicit initialization after declaration.

Short declaration inside functions using := declares and initializes in one step.

var imVar = 6
var imVar0 int = 7
imVar6, imVar7 := 8, 9 // short form

Constants

Constants are declared with const and must be assigned a value at declaration; they cannot use the short := syntax.

const imCnt int = 1
const imCnt1 = 1 // type inferred
// built‑in constants
true false iota nil

Functions

Declaration

func mult(i int, j int) int {
    return i * j
}
// when parameters share the same type, omit the type for all but the last
func mult(i, j int) int { return i * j }

Return Values

Go supports multiple return values and named return parameters.

func more(i, j int) (int, int) {
    return i * i, j * j
}
func retName(i, j int) (x, y int) {
    x = i * i
    y = j * j
    return // returns named values x and y
}

Key Rules

Semicolons are optional; the compiler inserts them automatically.

The opening brace { must be on the same line as the preceding statement.

Every declared variable must be used; otherwise compilation fails.

These fundamentals—packages, types, variables, constants, and functions—form the foundation for writing correct, idiomatic Go code and prepare developers for more advanced topics.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentGofunctionstypesVariablesprogramming basicsPackages
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.