The Go framework is a set of components that extend Go's built-in libraries to provide pre-built functionality (such as web development and database operations). Popular Go frameworks include Gin (web development), GORM (database operations), and RESTful (API management). Middleware is an interceptor pattern in the HTTP request processing chain, used to add functionality such as authentication or request logging without modifying the handler. Session management maintains session state by storing user data. You can use gorilla/sessions to manage sessions.
Go framework interview questions collection
Interview preparation is a crucial part of the software development process, especially for projects like Go Framework technology stack. This article compiles a collection of common Go framework interview questions and provides practical code examples to help you prepare for your next interview.
1. Explain the Go framework
Answer: The Go framework is an enhanced collection of built-in libraries in the Go language that provides pre-processing for common tasks. The components and structure of the build. These frameworks contain modules for functions such as web development, database operations, and API management.
2. List some popular Go frameworks
Answer:
- Web Development: Gin, Echo, Gorilla
- Database operations: GORM, xORM, SQLx
- API management: RESTful, Beego, Buffalo
3. Explain the role of middleware
Answer: Middleware is an interceptor pattern, used in Execute custom code within the HTTP request processing chain. It allows you to add functionality such as authentication, authorization, and request logging without modifying the handler itself.
4. How to use middleware in Gin
package main import ( "github.com/gin-gonic/gin" "golang.org/x/time/rate" "time" ) func main() { r := gin.Default() // 创建一个每秒允许 5 个请求的速率限制器 limiter := rate.NewLimiter(5, 5) // 定义一个中间件,它使用速率限制器检查每个请求 middleware := func(c *gin.Context) { if !limiter.Allow() { c.AbortWithStatus(http.StatusTooManyRequests) return } // 继续处理请求 c.Next() } // 将中间件应用到所有路由 r.Use(middleware) r.Run() }
5. Explain the role of session management in Go web applications
Answer: Session management allows applications to store and retrieve user information during a user session. It's essential for staying logged in, tracking shopping cart contents, and other user-specific data.
6. Use gorilla/sessions to manage sessions
package main import ( "fmt" "github.com/gorilla/sessions" "github.com/gorilla/mux" "log" "net/http" ) const ( SessionName = "session-name" CookieName = "session-id" Secret = "secret-key" ) var ( sessionStore *sessions.CookieStore ) func main() { // 创建 session 存储 sessionStore = sessions.NewCookieStore([]byte(Secret)) // 创建路由器 r := mux.NewRouter() r.HandleFunc("/login", loginHandler) r.HandleFunc("/profile", profileHandler) // 启动服务器 log.Fatal(http.ListenAndServe(":8080", r)) } // loginHandler 处理登录请求并创建 session func loginHandler(w http.ResponseWriter, r *http.Request) { // 创建 session session, err := sessionStore.Get(r, SessionName) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // 将用户 ID 设置为 session 数据 session.Values["user_id"] = 10 // 保存 session if err = sessionStore.Save(r, w, session); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // 重定向到个人资料页面 http.Redirect(w, r, "/profile", http.StatusFound) } // profileHandler 处理个人资料请求并检索 session 数据 func profileHandler(w http.ResponseWriter, r *http.Request) { // 获取 session session, err := sessionStore.Get(r, SessionName) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // 从 session 中检索用户 ID userID, ok := session.Values["user_id"].(int) if !ok { http.Error(w, "Invalid user ID in session", http.StatusBadRequest) return } // 使用用户 ID 渲染个人资料页面 fmt.Fprintf(w, "Your profile page, user ID: %d", userID) }
The above is the detailed content of Golang framework interview questions collection. For more information, please follow other related articles on the PHP Chinese website!