codes
How to Run a Go Program
As an example, let’s print “Hello World”:
1 | // main.go |
1 | Run directly |
Variable Declaration and Data Types
A variable is an identifier that points to an address in memory, typically used for storing data. A data type represents
the type of a variable, which determines how much space it occupies in memory. Assignment means placing a value into the
memory address pointed to by the variable. Later, you can use this variable to retrieve the data.
1 | package main |
Control Flow
Control flow defines the execution order of our program.
Sequential Execution
Sequential execution runs from top to bottom, which is the normal execution process of our program.
1 | package main |
Selection
选择就是根据满足不同的条件来执行不同的代码
选择的流程分为单分支和多分支
Single Branch
A single branch refers to having only one branch node and a single conditional check.
1 | package main |
Multiple Branches
Multiple branches involve several branch nodes and multiple condition checks.
1 | package main |
Loops
Loops represent a process of repeated execution.
1 | package main |
Functions
A function is essentially a closure with its own scope. We can define a block of code as a function, which can accept
zero or more inputs, execute the logic within the function body, and return zero or more outputs upon completion. Using
functions helps extract common logic and simplifies our code.
1 | package main |
Classes / Structs
Classes / Structs
A class is an abstract structure that contains data and methods (functions). We can define it, and to use it, we need to
instantiate it—essentially allocating space in memory to store it. The size of a class depends on what kind of data it
defines internally (e.g., int), and the compiler automatically determines how much space to allocate. A class can have
multiple instantiated objects—that is, multiple different variables—but these variables all conform to the same
structural definition.
1 | package main |
Interfaces
1 | package main |
Frameworks
To simplify development, programmers extract common logic into functions, or encapsulate them into classes and methods.
As this process continues and evolves, frameworks are born. In my understanding, a library is a collection of commonly
used functions and classes, allowing users to incorporate various libraries as they prefer. On the other hand, a
framework tends to define a set of project conventions and templates, where users develop based on the framework’s
predefined standards and structures. However, the distinction between the two terms is often blurred, and they are
frequently used interchangeably.
web
The programs we’ve written so far run locally. To allow users around the world to access them, we need to expose them
over the network. Web frameworks wrap the language’s native networking libraries and provide methods for building web
services. A commonly used web framework in Go is Gin.
1 | go get "github.com/gin-gonic/gin" |
1 | package main |
Visit localhost:8080/hello to see the output.
db
So far, we’ve been using variables to store data, but variable data resides in memory and will be lost once the program
stops. When restarting the program, the operating system may allocate a different memory space. Therefore, we need
persistent storage for our data, which leads us to database systems. A commonly used database framework in Go is GORM.
1 | go get "gorm.io/driver/sqlite" "gorm.io/gorm" "github.com/glebarez/sqlite" |
1 | package main |
Extensions
○ Multithreading / Microservices / Reflection - Dynamic Proxy / File Operations / Network Programming /
○ Framework Principles / Manual Implementation
○ WASM / gRPC