Basic part of golang:
Go cannot download and install git plug-in because the ip address of go agent cannot be accessed. You can change the following address:
Proxy is used by default golang. Org, not accessible at home
go>go get -u github.com/go-sql-driver/mysql
go>go env -w GOPROXY=https://goproxy.cn
goweb part:
Four link modes of goweb:
net/http package
package main import ( "fmt" "net/http" ) func handel(w http.ResponseWriter, r *http.Request) { // fmt.Fprintln(w, "helloword !!!", r.URL) fmt.Fprintln(w, "Using multiplexer helloword !!!", r.URL) } func main() { //1. The first //The simplest and most commonly used way // http.HandleFunc("/", handel) // http.ListenAndServe(":8080", nil) //2. The second //Create a multiplexer to call your own handel mux := http.NewServeMux() mux.HandleFunc("/", handel) http.ListenAndServe(":8080", mux) }
package main import ( "fmt" "net/http" "time" ) type MyHandel struct{} func (this *MyHandel) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Through their own handel Processing data...", r.URL) fmt.Fprintln(w, "Process data through detailed configuration") } func main() { //3. The third: //Simple configuration call var myHandel MyHandel // http.Handle("/myhandel", &myHandel) // http.ListenAndServe(":8080", nil) //4. The fourth: //By configuring the detailed server call server := http.Server{ Addr: ":8080", Handler: &myHandel, ReadTimeout: 2 * time.Second, } server.ListenAndServe() }
goweb linked database:
database/sql package
Install the third-party driver package
demo:
There are two ways to add, delete and modify:
1. With precompiling
2. Without precompiling
3.
Query sql
Check one:
api:
Check all:
testing package:
1. The file name must be xxx_testing.go
2. The test file must be in the same folder as the tested file
3. Naming rules of func function name: it is suggested that it must start with Test, and the second letter should be named in capital hump, such as TestXxx and TestAddUser
4. Operation command line: go test
Example:
The second execution method:
Execution of subroutines
Third:
TestMain mode:
go test - v print details
package main import ( "fmt" "testing" ) //TestMain is called before TestAddUser func TestMain(m *testing.M) { fmt.Println("TestMain program ...Called") m.Run() } //Must start with Test + uppercase letter + other TestAddUser //File name must be xxx_test.go with test Go end func TestAddUser(t *testing.T) { fmt.Println("TestAddUser ...Called") t.Run("testUser", testUser) } func testUser(t *testing.T) { fmt.Println("testUser subroutine ...Called") }
Operation results:
go test :
go test -v
3. Obtain request data
api:
Results:
Get request parameters:
When testing the paraseForm, close other tests above to prevent any impact
api:
Results:
Response request:
Corresponding page:
json response:
Redirect:
Results:
go's template engine:
api:
Background:
Interface
backstage:
result:
go processing static resources:
Book City Project:
Initialize database system:
Create table
testing
Registration:
Action:
example:
backstage:
Reception:Iteration:
example:
backstage:
Reception:
Traversal map
Traverse pipeline:
example:
Front end:
result:
Include actions:
Define actions:
Block action:
Continue the book city project:
Create database table:
New book structure
Background:
Test:
Modify html call href
Add books:
control:
dao main:
Test:
Front end:
Results:
Delete:
dao:
test:
go test
main
control:
Results:
Modification:
dao:
Check a Book:
test: control: Interface:
Merge, add and update pages. The difference is whether it is the id of the book. If it is the id of the book, it is updated. If it is not, it is added
Delete the add page and modify the update page below:
Background part:
main:
Pagination
Database Paging principle:
Create paging structure
Implement paging function:
main:
Go to the home page:
Ditto similar
The query with condition is the same as the query with condition. It is only necessary to add the corresponding condition statement in the sql statement, and others remain unchanged
7. Session control
Two ways to set cookie s
Get cookie
Page judgment:
Logout:
/ / determine repeated login:
Shopping cart:
item struct structure of each
struct of shopping cart
The addition, deletion and modification of shopping cart are the same as that of book
The addition, deletion and modification of the order are the same as the addition, deletion and modification of the book