Home > Article > Backend Development > Learn web development skills and practical experience in Go language
To master the Web development skills and practical experience of Go language, specific code examples are required
Introduction:
With the rapid development of the Internet, Web development technology is also becoming more and more important. have attracted more and more attention from people. The Go language, as a cross-platform programming language, is increasingly favored by developers in the field of Web development due to its high efficiency, reliability and easy maintenance. This article will summarize the web development skills and practical experience of Go language, and provide specific code examples to help readers quickly master and apply these skills.
1. Routing and Controller
In the web development of Go language, routing and controller are the most basic concepts. Routing is mainly responsible for mapping URLs to corresponding processing functions, while controllers are responsible for processing specific business logic. Here is a basic route and controller example:
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/hello", helloHandler) http.ListenAndServe(":8080", nil) } func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") }
With the http.HandleFunc
function, we can combine the /hello
path with helloHandler
function to map. In the helloHandler
function, we use http.ResponseWriter
to build the response content and fmt.Fprintf
to write the content into the response.
2. Template engine
In Web development, it is often necessary to present dynamic data to users in a readable form, which requires the use of a template engine. The Go language has a built-in html/template
package, which provides us with powerful template engine functions. The following is an example of using a template engine:
package main import ( "html/template" "net/http" ) type User struct { Name string Email string } func main() { http.HandleFunc("/user", userHandler) http.ListenAndServe(":8080", nil) } func userHandler(w http.ResponseWriter, r *http.Request) { user := User{"John", "john@example.com"} tmpl, err := template.ParseFiles("user.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = tmpl.Execute(w, user) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
In the above example, we first define a User
structure to store user information. Then, load a template file named user.html
through the template.ParseFiles
function. Finally, use the Execute
function to replace the placeholders in the template with real data and respond to the client with the generated HTML.
3. Database Operation
In actual Web development, it is often necessary to interact with the database. Go language has built-in database/sql
package, which can easily connect and operate with various databases. The following is an example using a MySQL database:
package main import ( "database/sql" "fmt" "net/http" _ "github.com/go-sql-driver/mysql" ) type User struct { ID int Name string Email string } func main() { http.HandleFunc("/user", userHandler) http.ListenAndServe(":8080", nil) } func userHandler(w http.ResponseWriter, r *http.Request) { db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/dbname") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer db.Close() rows, err := db.Query("SELECT id, name, email FROM users") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer rows.Close() var users []User for rows.Next() { var user User err := rows.Scan(&user.ID, &user.Name, &user.Email) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } users = append(users, user) } for _, user := range users { fmt.Fprintf(w, "ID: %d, Name: %s, Email: %s ", user.ID, user.Name, user.Email) } }
In the above example, we first connect to the MySQL database using the sql.Open
function. Then, execute the SQL statement through the db.Query
function, and use the rows.Scan
function to map the query results to the User
structure. Finally, use the fmt.Fprintf
function to output the query results to the response.
Conclusion:
This article introduces several techniques and practical experiences commonly used in Go language web development, and provides specific code examples. By mastering these skills, readers can carry out web development in Go language more efficiently and use it flexibly in actual projects. I hope this article will be helpful to you, and may you achieve greater success in the field of web development in Go language!
The above is the detailed content of Learn web development skills and practical experience in Go language. For more information, please follow other related articles on the PHP Chinese website!