Division of labor and collaboration and functional reuse
1. How to realize the division of labor
-
Modular programming can not only improve the clarity of the code structure, but also promote the level of team collaboration and improve development efficiency
-
create project
- Create a project folder to specify the specific location where the project files are stored
- Create a project folder under the
GOPATH
following folder, such assrc
等差数列
-
According to the division of labor, establish their own working directory for each developer, respectively
main
,input
,sum
,output
-
Each developer right-clicks on their respective working directory\selects New File to create their own go files, which are
main.go
,input.go
,sum.go
andoutput.go
-
Other working directories and the main folder are in the same level directory, so the relative path should be written when referencing the package
src
; -
package input//input.go import ( "fmt" ) //Input 输入函数 func Input() (a1 int, num int, dis int) { //()表示无参数 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 求和函数 func Sum(a1,num,dis int)(ans int){ ans=a1*num+num*(num-1)*dis/2 return }
-
package output//output.go import ( "fmt" ) //Output 输出函数 func Output(ans int){ fmt.Println("the answer is:",ans) }
-
package main import ( "等差数列/input" "等差数列/sum" "等差数列/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 testing
-
In daily development, testing is indispensable
-
The Go language comes with a lightweight testing framework testing and its own go test command to implement unit testing and performance testing
-
The test case file name must
*_test.go
end with *, preferably the same as the main file name of the test file, for easy reading -
The test case code is stored in the same path as the tested code
-
The standard test parameters are:
t *testing.T
-
testing provides support for automated testing of Go packages. With
go test
the command, any function of the following form can be automatically executed:func TestXxx(*testing.T)
where
Xxx
can be any alphanumeric string (except [az] as the first letter) that identifies the test routine. -
Within these functions, use Error, Fail, or related methods to signal failure.
-
In a test case file, there can be multiple test case functions
-
PASS indicates that the test case runs successfully, and FAIL indicates that the test case fails to run
-
When an error occurs, you can use
t.Fatalf
to format the output error message and exit the program -
t.Logf
The method can output the corresponding log -
(1)
cmd>go test
[If the operation is correct, there is no log, and when there is an error, the log will be output]
(2)cmd>go test -v
[The operation is correct or the error will output the log]
package sum//sum包
//Sum 求和函数
func Sum(a1,num,dis int)(ans int){
ans=a1*num+num*(num-1)*dis/2
return
}
package sum//对sum包的单元测试
import "testing"//通过testing包实现自动化测试
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\分工协作\eqd\sum>go test PASS ok 分工协作/eqd/sum 0.159s
E:\file\golang\goproject\src\分工协作\eqd\sum>go test -v === RUN TestSum TestSum: sum_test.go:9: success --- PASS: TestSum (0.00s) PASS ok 分工协作/eqd/sum 0.163s
3. How to conduct performance testing
- Mainly evaluate the response time, load and other indicators in a stressful environment
- Created
sum.go
performance test case codesum_b_test.go
- The test function name must
Benchmark
start with , and the standard performance test parameters are:b *testing.B
- Execute
go test –bench=.*
performance testing
package sum//对sum包的性能测试
import "testing"
func BenchmarkSum(b *testing.B){
for i:=0;i<b.N;i++{
//b.N为默认参数
Sum(1,100,1)
}
}
E:\file\golang\goproject\src\等差数列\sum> go test -bench=".*"
goos: windows goarch: amd64 pkg: 等差数列/sum BenchmarkSum-8 1000000000 0.260 ns/op PASS ok 等差数列/sum 0.459s
Run 1 billion times, time spent
0.459s
,0.260ns/op
- Perform CPU and memory status analysis - text format
- first
go test -bench=".*" -cpuprofile=sum.prof
- Generate executable test files
sum.test.exe
and CPU performance filessum.prof
- Then use
go tool pprof
the tool to enterpprof
the command mode to analyze the data:go tool pprof sum.test.exe sum.prof
- Finally, enter the text command to list status information in text form
- quit
- tree /f can see the file tree
- first
- Perform CPU and memory state analysis - state diagram method
- first
go test -bench=".*" -cpuprofile=sum.prof
- Then use
go tool pprof
the tool to enterpprof
the command mode to analyze the data:go tool pprof sum.test.exe sum.prof
- Enter the web command to view the performance analysis graph in IE
cpu
(you need to install it in advancegraphviz
to view it graphically in IE)
- first
4. How to integrate?
- In the project development process, after the unit test and performance test are passed, how to summarize the various parts of the code to form a complete system? How can each project member obtain the complete code?
- Recommendations for collaborative development tools
- Source Control:
CVS/SVN、git 和github
- Bug Tracking:
Bugzilla, Trac, Roundup
- communication tools:
maillist, IM, Forum, IRC, Wiki
- Collaborative development platform:
sourceforge, BaseCamp
- Source Control:
5. How to compile and execute the whole system?
- The last directory of the go install package source code storage path
- go install: Mainly used to generate libraries and tools.
- One is to 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 an executable file (with main package), and put the executable file into the bin directory (
$GOPATH/bin
).
- One is to compile the package file (no main package), and put the compiled package file in the pkg directory (
- like
6. Code reuse
-
package main//findmax main.go import ( "fmt" "代码复用/maxthree" ) func main(){ fmt.Println(maxthree.Maxthree(3,4,5)) }
-
package maxthree//maxthree maxthree.go import "代码复用/maxtwo" //Maxthree 找最大 func Maxthree(a,b,c int)int{ return maxtwo.Maxtwo(maxtwo.Maxtwo(a,b),c) }
-
package maxtwo//maxtwo maxtwo.go //Maxtwo 选出大者 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 选出大者
func Maxtwo (a,b int)int{
if a>=b{
return a
}
return b
}