- Definition: A combination of values, variables, operators, and function calls that evaluates to a single value.
- Examples:
2 + 2
(evaluates to4
)x * y
(evaluates to the product ofx
andy
)time.Second * 10
(evaluates to atime.Duration
value of 10 seconds)
- Usage: Expressions are used to compute values. They can appear within statements, assignments, or function arguments.
- Definition: A complete unit of execution that performs an action, like assigning a value, calling a function, or controlling the flow of a program.
- Examples:
- Assignment:
x := 5
- Function call:
fmt.Println("Hello, World!")
- Control flow:
if x > 10 { fmt.Println("x is large") }
- Assignment:
- Types of Statements:
- Expression Statement: An expression used as a standalone statement (e.g.,
x + y
is an expression;x + y;
on its own would be invalid in Go, butfmt.Println(x + y)
is a valid statement). - Control Statement: Includes
if
,for
,switch
, etc. - Declaration Statement: Declares variables or constants, e.g.,
var a int
.
- Expression Statement: An expression used as a standalone statement (e.g.,
- Definition: A special instruction to the compiler or tooling, modifying behavior at compile time or runtime.
- Examples:
//go:generate
: Tells the Go toolchain to generate code during build processes.- Build constraints:
// +build linux
ensures code is only compiled on Linux systems.
- Usage: Directives are typically comments prefixed with
//
, interpreted by Go tools.
- Definition: Introduces new identifiers (variables, constants, types, or functions) into the program.
- Examples:
- Variable declaration:
var x int
- Constant declaration:
const Pi = 3.14
- Type declaration:
type Point struct { X, Y int }
- Function declaration:
func Add(a, b int) int { return a + b }
- Variable declaration:
- Definition: Reserved words in a language with predefined meanings that cannot be used as identifiers.
- Examples:
if
,else
,func
,return
,var
,const
, etc.
- Usage: Keywords form the syntax and structure of the language.
- Definition: A sequence of statements enclosed in curly braces
{}
. - Examples:
func main() { fmt.Println("Hello, World!") // This is a block }
- Usage: Blocks are used to group statements together in functions, loops, or conditionals.
- Definition: A fixed value written directly in the code.
- Examples:
- Numbers:
42
,3.14
- Strings:
"hello"
- Booleans:
true
,false
- Numbers:
- Usage: Literals are used to represent constant values in a program.
- Definition: A symbol that performs operations on variables or values.
- Examples:
- Arithmetic:
+
,-
,*
,/
- Logical:
&&
,||
,!
- Relational:
==
,!=
,<
,>
- Arithmetic:
- Usage: Operators are used within expressions to compute values or make comparisons.
- Definition: A classification that defines the kind of data a variable or value can hold.
- Examples:
- Primitive types:
int
,float64
,string
- Composite types:
struct
,array
,slice
,map
- Primitive types:
- Usage: Types ensure variables are used consistently and define the operations applicable to them.
- Definition: A block of code that performs a specific task and can be reused.
- Examples:
func Add(a, b int) int { return a + b }
- Usage: Functions encapsulate logic and make the code reusable and modular.
- Definition: A collection of related Go files grouped together to provide reusable functionality.
- Examples:
- Standard packages:
fmt
,time
,strings
- Custom packages:
mypackage
- Standard packages:
- Usage: Packages allow modular programming and reuse of code across projects.
- Definition: Brings a package into the current file for use.
- Examples:
import "fmt"
- Usage: Imports allow access to external or standard library functionality.
- Definition: A human-readable note in the code that is ignored by the compiler.
- Examples:
- Single-line:
// This is a comment
- Multi-line:
/* This is a multi-line comment */
- Single-line:
- Usage: Used for documentation or explanations.
// This is a package declaration (declaration)
package main
import "fmt" // Import statement
// Function declaration
func main() {
var x int = 10 // Declaration statement
const y = 20 // Constant declaration
// Expression within an assignment statement
result := x + y
// Control flow statement
if result > 15 {
fmt.Println("Result is greater than 15") // Expression statement
}
// Function call within a block
fmt.Println("End of program") // Directive example: //go:generate could appear here
}