Go Basic Syntax
Go language has simple and clear syntax. This chapter will detail the basic syntax rules of Go language, including identifiers, keywords, literals, operators, and other core concepts.
🔤 Identifiers
Identifier Rules
Identifiers in Go are used to name variables, types, functions, etc.
Naming Rules
- Character Composition: Letters, numbers, underscores
- First Character: Must be a letter or underscore
- Case Sensitive:
nameandNameare different identifiers - Length Limit: No limit, but reasonable length is recommended
Naming Conventions
Go Naming Conventions
Visibility Rules
🔑 Keywords
Go Reserved Words
Go has 25 keywords that cannot be used as identifiers:
Keyword Categories
1. Declaration Keywords
2. Control Flow Keywords
3. Concurrency Keywords
💡 Literals
Numeric Literals
Integer Literals
Floating-Point Literals
String Literals
Interpreted Strings
Raw Strings
Character Literals
Boolean Literals
📝 Syntax Structure
Statement Separation
Code Blocks
🔧 Expressions and Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Bit Operators
Assignment Operators
Increment/Decrement Operators
🎯 Operator Precedence
From highest to lowest priority:
Precedence Example
🔄 Type Inference
var Declaration Type Inference
Short Variable Declaration
📋 Blank Identifier
Discarding Unused Values
🎯 Practical Example
Comprehensive Syntax Example
🎓 Summary
In this chapter, we learned the basic syntax of Go language:
- ✅ Identifiers: Naming rules and visibility standards
- ✅ Keywords: 25 reserved words and their uses
- ✅ Literals: Numeric, string, character, boolean literals
- ✅ Operators: Arithmetic, comparison, logical, bitwise, assignment operators
- ✅ Syntax Structure: Statement separation, code blocks, expressions
- ✅ Type Inference: Automatic type inference mechanism
Mastering these basic syntaxes is an important foundation for further learning Go.
Next, we will learn Go Data Types to deeply understand Go's type system.
::: tip Syntax Tips
- Go language syntax is designed to be simple and clear, reducing unnecessary complexity
- Brace placement has strict requirements, left brace cannot be on a new line alone
- Use gofmt tool to automatically format code and maintain consistent code style :::