Variable declaration:
-
Variable declaration: In Go, variables must be declared first and then used. Declare a variable using the
var
keyword, followed by the variable name and type, as follows:var age int
This line of code declares an integer variable named
age
. -
Variable initialization: You can assign an initial value to a variable at declaration time, or you can omit the type and let Go automatically infer the type:
var name string = "John" var age = 30 // 类型自动推断为int
-
Short variable declaration: For local variables, you can use the
:=
operator to make short variable declarations. This approach does not require an explicit type specification:name := "John"
Go will automatically infer that the type of
name
is string. -
Constant declaration: Constants are declared using the
const
keyword. Once declared, their values cannot be changed:const pi = 3.14
The value of a constant is determined at compile time and must be a constant expression.
Conditions and loops:
-
if statement: The
if
statement in Go is used to execute conditional statements. It can be used with an optionalelse
statement:if age >= 18 { fmt.Println("You are an adult") } else { fmt.Println("You are not an adult") }
-
switch statement: Go's
switch
statement is very flexible and can be used for multiple conditions without usingbreak
Keywords:switch day { case "Monday": fmt.Println("It's Monday!") case "Tuesday": fmt.Println("It's Tuesday!") default: fmt.Println("It's another day of the week") }
-
for loop: Go's
for
loop has three basic uses:-
Basic
for
Loops:for i := 0; i < 5; i++ { fmt.Println(i) }
-
for
Loops used as while:sum := 0 for sum < 10 { sum += 1 }
-
for
Loop over a collection (such as a slice or map):numbers := []int{ 1, 2, 3, 4, 5} for index, value := range numbers { fmt.Printf("Index: %d, Value: %d\n", index, value) }
-
-
break and continue: In a loop, you can use the
break
keyword to exit the loop immediately, andcontinue
keyword is used to jump to the next loop iteration.
When it comes to Go language functions, structures and interfaces, here is a detailed explanation:
function:
In Go, a function is a reusable block of code that performs a specific task or operation. Functions have the following characteristics:
-
Define functions: Use the
func
keyword to define functions. A function contains a function name, a parameter list, and a return value list.func add(a, b int) int { return a + b }
-
Parameters and return values: A function can accept parameters and can have one or more return values. In the example above, the
add
function accepts two integer arguments and returns an integer. -
Function call: To call a function, simply use the function name and the required argument list.
result := add(5, 3)
-
Multiple return values: Go supports functions with multiple return values, which means that a function can return multiple values.
func divide(a, b int) (int, error) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil }
-
Anonymous functions: In Go, you can also create anonymous functions, which are functions without a function name and are typically used to be defined and used inside a function.
func main() { add := func(a, b int) int { return a + b } result := add(2, 3) fmt.Println(result) }
Structure:
The structure in Go language is a user-defined composite data type that is used to combine multiple fields to represent a data structure. Structures have the following properties:
-
Define the structure: Use the
type
keyword to define the structure. Fields of a structure usually begin with a capital letter to indicate that they are exported (can be accessed in other packages).type Person struct { FirstName string LastName string Age int }
-
Create a structure instance: Create an instance using the structure type and then initialize the fields.
person := Person{ FirstName: "John", LastName: "Doe", Age: 30, }
-
Access structure fields: Use the
.
operator to access the value of a structure field.fmt.Println(person.FirstName) // 输出 "John"
-
Anonymous structures: You can also create anonymous structures for temporary storage of data.
person := struct { FirstName string LastName string }{ FirstName: "John", LastName: "Doe", }
-
Nested structures: Structures can be nested within other structures to build more complex data structures.
interface:
An interface is an abstract type in Go language that defines a set of method signatures but no specific implementation. The interface has the following characteristics:
-
Define the interface: Use the
type
keyword to define the interface.type Writer interface { Write([]byte) (int, error) }
-
Implementing the interface: Any type that implements all methods defined in the interface is considered to implement the interface. Go uses implicit interface implementation.
-
Interface type variables: You can create variables of interface type and assign any value that implements the interface to these variables.
var w Writer w = os.Stdout
-
Multiple interface implementation: A type can implement multiple interfaces at the same time.
-
Empty interface: The empty interface
interface{}
does not contain any methods and therefore can represent any type of value.var emptyInterface interface{ } emptyInterface = 42 emptyInterface = "Hello"
-
Type assertion: Use type assertion to check the underlying type of an interface type variable and obtain its value.
value, ok := emptyInterface.(int) if ok { fmt.Println("It's an integer:", value) }
Good book recommendation in this issue is "Proficient in Go Language"
Order link:https://item.jd.com/13543938.html