Lesson 1: Go Basics¶
Big Picture¶
flowchart TD
A["Go source file (.go)"] --> B["package declaration"]
B --> C["import statements"]
C --> D["func main() — program entry point"]
D --> E["fmt.Println → stdout"]
Every executable Go program follows this skeleton: declare the package, import what you need, and put your logic in main().
Printing¶
fmt.Println()— print with newlinefmt.Print()— print without newlinefmt.Printf()— formatted printing
Go Naming and Access Conventions¶
Go uses the first letter of a name to control whether other packages can access it:
- Capitalized names are exported:
Println,NewServer, andUsercan be accessed from another package. - Lowercase names are package-private:
main,calculateTotal, andusercan only be accessed from within the same package. - Capitalize a function, type, variable, constant, method, or struct field only when it is part of the package's public API.
-
Prefer lowercase names for implementation details that callers do not need.
-
Use a dot to access an exported identifier from an imported package:
- Here,
fmtis the package name andPrintlnis an exported function.mainstays lowercase because the Go runtime looks for that exact special function name; it is not called from another package.
- Here,
-
The same export rule applies to struct fields:
Common naming conventions:
- Use
camelCasefor unexported names andPascalCasefor exported names; do not use underscores. - Keep package names short, lowercase, and singular, such as
http,json, orprofile. - Avoid repeating the package name: prefer
http.Serveroverhttp.HTTPServer. - Acronyms normally keep consistent capitalization:
userID,UserID,HTTPClient, andServeHTTP. - Use short names like
iin small scopes, but descriptive names likecustomerCountin larger scopes.
Code Walkthrough¶
Exercises
- Modify the hello message.
- Try
fmt.Printvsfmt.Println— what's different? - Use
fmt.Printfwith%d,%s, and%fformat specifiers.
Check Your Understanding¶
#
What must every executable Go program declare at the top of its entry file?
#
Why can't the code/ package have func main() in every lesson file?
#
Which fmt function prints a formatted string like "Age: %d"?