type
status
date
slug
summary
tags
category
icon
password
route注册路由,文件夹下实现function
open文件之后,要接一个defer →make sure file closed
how to understand c *gin.Context
gin.Context is a struct that forms the core of Gin's request handling.
The gin.Context structure contains both the http.Request and http.Response that a standard http.Handler would use, along with convenient methods and shortcuts for manipulating them.
The Gin engine creates and reuses these contexts, similar to how http.Server creates http.Request objects for standard http.Handlers.
The engine passes the context to its handlers, which you write and attach to a router. A Gin handler is simply a function that takes a gin.Context pointer as its only argument and returns nothing.
why pointer?
Using a pointer passes the memory address of the gin.Context object instead of copying the entire object. This approach is more efficient, especially for large objects. Additionally, when methods are called on the context pointer, they can modify the underlying gin.Context object—essential for setting headers and writing responses.
c.JSON(404, gin.H{"error": "Log file not found"}):
make
make is used to initialize slices, maps, and channels in Go.
make is similar to new in Java, but with some key differences.
For maps, make(map[string][]string) creates a map that's equivalent to Java's HashMap<String, List>.
Slices in Go are dynamic arrays. Using make([]int, 5) creates a slice with length 5, similar to new ArrayList(5), but Go slices have both length and capacity properties.
Channels are Go-specific constructs. While Java has no direct equivalent, BlockingQueue provides similar functionality.
A map without make initialization is nil—attempting to add elements to it causes a panic, just like a NullPointerException in Java.
RPC
WHY RPC?
In a monolithic application, functions within different modules call each other directly within the same process. However, in a microservices architecture, applications are broken down into independent services that run in separate processes, often on different machines. Direct function calls across these services are not possible. Services need to communicate over a network.
RPC is designed to make networked service calls feel as simple and natural as local function calls. It hides the complexities of network communication, allowing developers to focus on business logic rather than network programming details
err = client.Call(utils.NewServiceContext(c), method, &args, &reply)
大小写进行辨别输出
route.GET("", user, List)
route.POST("", admin, Create) 这种可以直接调用?大写的明明是方法没看到是在哪里传进来的?
在同一个包内的文件可以相互访问,无需显式导入。这意味着,如果 route.go 和 user.go 都在 user 包中,route.go 可以直接调用 List、Create 等函数,而无需进行额外的导入。
这里涉及到的就是说:
导出规则(Exported Identifiers)
A. 大写字母开头的标识符
在 Go 中,以大写字母开头的标识符(函数名、变量名、结构体名等)是 导出的(exported),这意味着它们可以被其他包访问。
B. 小写字母开头的标识符
以小写字母开头的标识符是 未导出的(unexported),只能在同一个包内使用,外部包无法访问。
C. 导入其他包的函数
如果 List、Create 等函数来自其他包(不是当前包),则需要在文件顶部导入相应的包,并使用包名作为前缀