Go Basic Syntax
Go - Basic Syntax
Section titled “Go - Basic Syntax”This chapter introduces the fundamental building blocks of a Go program.
Tokens in Go
Section titled “Tokens in Go”A Go program is composed of tokens. A token can be a keyword, an identifier, a constant, a string literal, or a symbol. Consider the following Go statement:
package main
import "fmt"
func main() { fmt.Println("Hello, World!")}This program uses several tokens:
Keywords: package, import, funcIdentifiers: main, fmt, PrintlnString Literal: "Hello, World!"Symbols: ., (, )Semicolons and Line Endings
Section titled “Semicolons and Line Endings”In Go, the newline character generally acts as a statement terminator. Unlike languages like C or Java, you usually don’t need to place semicolons (;) at the end of each statement. The Go compiler automatically inserts semicolons based on lexical rules.
For example, these are two separate statements:
fmt.Println("Hello, World!")fmt.Println("Learning Go syntax!")However, if you want to write multiple statements on a single line, you must separate them with semicolons:
fmt.Print("First"); fmt.Print("Second") // Semicolon required hereComments
Section titled “Comments”Comments are explanatory text ignored by the compiler. Go supports two types of comments:
// This is a single-line commentvar message = "Hello" // Comment after code/*This is a multi-line comment.It can span several lines.*/Comments cannot be nested. They also don’t affect the interpretation of string or character literals.
Tip: Use comments prefixed with the name of the declared item (e.g., // User represents a user or /* Package config handles configuration */) right before package, type, function, or variable declarations to generate documentation using the go doc tool. These are called doc comments.
Identifiers
Section titled “Identifiers”An identifier is a name used to identify variables, functions, types, constants, etc. Rules for identifiers:
Examples of valid identifiers:
userService _tempVar isValid MAX_RETRIES piValueVisibility (Exported vs. Unexported): A crucial convention in Go is that identifiers starting with an uppercase letter are exported (publicly accessible from other packages). Identifiers starting with a lowercase letter or underscore are unexported (private to the package where they are defined).
var InternalCounter int // Unexported (private to this package)const ExportedConstant = 3.14 // Exported (public)Keywords
Section titled “Keywords”Keywords are reserved words with special meaning in the Go language. They cannot be used as identifiers. Here is the list of Go keywords:
| break | default | func | interface | select |
|---|---|---|---|---|
| case | defer | go | map | struct |
| chan | else | goto | package | switch |
| const | fallthrough | if | range | type |
| continue | for | import | return | var |
Whitespace in Go
Section titled “Whitespace in Go”Whitespace refers to spaces, tabs, and newline characters. Go uses whitespace to separate tokens. Blank lines (containing only whitespace and possibly comments) are ignored by the compiler.
In this statement, whitespace is necessary between var, age, and int:
var age intIn this statement, whitespace around = and + is optional but improves readability:
total = items + tax // Calculate the totalGo Best Practice: Use the gofmt tool (or go fmt) to automatically format your code according to standard Go conventions. Most Go development environments integrate this tool, ensuring consistent whitespace and style.