golang learning -- division of labor, cooperation and function reuse

Posted by maddog720 on Fri, 14 Jan 2022 09:16:45 +0100

Division of labor, cooperation and function reuse

1. How to realize division of labor

  • Modular programming can not only improve the clarity of code structure, but also promote the level of team cooperation and improve development efficiency

  • Build project

    • Establish a project folder and specify the specific location of project documents
    • Create a project folder under src folder under GOPATH, such as arithmetic sequence
  • According to the division of labor, establish their own working directories for each developer, namely main, input, sum and output

  • Each developer right-click their own working directory \ select New File to create their own go files, which are main go,input.go,sum.go and output go

  • If other working directories and main folders are in the same level directory, the relative path relative to src should be written when referring to the package;

  • package input//input.go
    
    import (
    	"fmt"
    )
    
    //Input function
    func Input() (a1 int, num int, dis int) { //() indicates no parameters
    	fmt.Println("input a0")
    	fmt.Scanln(&a1)
    	fmt.Println("input num")
    	fmt.Scanln(&num)
    	fmt.Println("input dis")
    	fmt.Scanln(&dis)
    	return
    }
    
    
  • package sum//sum.go
    
    //Sum summation function 
    func Sum(a1,num,dis int)(ans int){
    	ans=a1*num+num*(num-1)*dis/2
    	return 
    }
    
  • package output//output.go
    
    import (
    	"fmt"
    )
    //Output function
    func Output(ans int){
    	fmt.Println("the answer is:",ans)
    }
    
  • package main
    
    import (
    	"Arithmetic sequence/input"
    	"Arithmetic sequence/sum"
    	"Arithmetic sequence/output"
    )
    
    func main(){
    	a1,num,dis:=input.Input()
    	ans:=sum.Sum(a1,num,dis)
    	output.Output(ans)
    }
    

    input a1 15 input num 20 input dis 3 the answer is: 870

2. How to conduct unit test

  • In daily development, testing is indispensable

  • The Go language comes with a lightweight testing framework testing and a built-in go test command to implement unit testing and performance testing

  • The test case file name must be*_ test. At the end of go, * is better to be the same as the main file name of the test file for easy reading

  • The storage path of test case code and tested code is the same

  • The standard test parameters are: T * testing T

  • Testing provides support for automated testing of Go packages. With the go test command, you can automatically execute any of the following functions:

    func TestXxx(*testing.T)
    

    Where Xxx can be any alphanumeric string (but the first letter cannot be [a-z]) to identify the test routine.

  • In these functions, Error, Fail or related methods are used to signal failure.

  • There can be multiple test case functions in a test case file

  • PASS indicates that the test case runs successfully, and FAIL indicates that the test case fails

  • When an error occurs, you can use t.Fatalf to format the output error message and exit the program

  • t. The logf method can output the corresponding log

  • (1) CMD > go test [if the operation is correct and there is no log, the log will be output in case of error]
    (2) CMD > go test - v [if the operation is correct or wrong, the log will be output]

package sum//sum package

//Sum summation function 
func Sum(a1,num,dis int)(ans int){
	ans=a1*num+num*(num-1)*dis/2
	return 
}
package sum//Unit test of sum package

import "testing"//Automated testing through testing package
func TestSum(t *testing.T){
	result:=Sum(1,100,1)
	if result!=5050{
		t.Fatalf("wrong")
	}else{
		t.Logf("success")
	}
}

E:\file\golang\goproject\src \ division and collaboration \ eqd \ sum > go test pass OK division and collaboration / eqd/sum 0.159s

E:\file\golang\goproject\src \ division of labor and collaboration \ eqd \ sum > go test - v = = = run testSum testSum: Sum_ test. Go: 9: success --- pass: testSum (0.00s) pass OK division of labor and cooperation / eqd/sum 0.163s

3. How to conduct performance test

  • It mainly evaluates the response time, load and other indicators under pressure environment
  • Create sum Go performance test case code sum_b_test.go
  • The test function name must start with Benchmark, and the standard performance test parameters are: B * testing B
  • Execute go test – bench =* Conduct performance test
    package sum//Performance test of sum package

    import "testing"
    func BenchmarkSum(b *testing.B){
        for i:=0;i<b.N;i++{//b.N is the default parameter
            Sum(1,100,1)
        }
    }

E:\file\golang\goproject\src \ arithmetic sequence \ sum > go test - bench = ". *"

goos: windows goarch: amd64 pkg: arithmetic sequence / sum BenchmarkSum-8 1000000000 0.260 ns/op PASS ok arithmetic sequence / sum 0.459s

Run 1 billion times, time 0.459s,0.260ns/op

  • CPU and memory status analysis - text format
    • First, go test - bench = ". *" - cpupprofile = sum prof
    • Generate executable test file sum test. Exe and CPU performance file sum prof
    • Then use the go tool pprof tool to enter the pprof command mode to analyze the data: go tool pprof sum test. exe sum. prof
    • Finally, enter the text command to list the status information in text form
    • quit exit
    • tree /f can see the file tree
  • CPU and memory state analysis - state diagram mode
    • First, go test - bench = ". *" - cpupprofile = sum prof
    • Then use the go tool pprof tool to enter the pprof command mode to analyze the data: go tool pprof sum test. exe sum. prof
    • Enter the web command and view the cpu performance analysis chart in IE (you need to install graphviz in advance to view it graphically in IE)

4. How to integrate?

  • In the process of project R & D, after the unit test and performance test are passed, how to summarize each part of the code to form a complete system? How can each project member get the complete code?
  • Recommendation of collaborative development tools
    • Source Control: CVS/SVN, git and github
    • Bug Tracking: Bugzilla, Trac, Roundup
    • Communication tools: maillist, IM, Forum, IRC, Wiki
    • Collaborative development platform: sourceforge, BaseCamp

5. How to compile and execute the whole system?

  • The last directory of the source code storage path of the go install package
  • go install: mainly used to generate libraries and tools.
    • First, compile the package file (no main package), and put the compiled package file in the pkg directory ($GOPATH/pkg).
    • The second is to compile and generate the executable file (with main package) and put the executable file in the bin directory ($GOPATH/bin).
  • as

6. Code reuse

  •     package main//findmax  main.go
    
        import (
            "fmt"
            "code reuse/maxthree"
        )
    
        func main(){
            fmt.Println(maxthree.Maxthree(3,4,5))
        }
    
  •     package maxthree//maxthree   maxthree.go
        import "code reuse/maxtwo"
        //Maxthree find maximum
        func Maxthree(a,b,c int)int{
            return maxtwo.Maxtwo(maxtwo.Maxtwo(a,b),c)
        }
    
  •     package maxtwo//maxtwo   maxtwo.go
        //Maxtwo chooses the big one
        func Maxtwo (a,b int)int{
            if a>=b{
                return a
            }
            return b
        }
    

o(maxtwo.Maxtwo(a,b),c)
}

+ ```go
    package maxtwo//maxtwo   maxtwo.go
    //Maxtwo chooses the big one
    func Maxtwo (a,b int)int{
        if a>=b{
            return a
        }
        return b
    }

Topics: Go