Reference: https://zhuanlan.zhihu.com/p/109828249
1, create new project "test0_math"
$ cd ~/project
$ mkdir test0_math
2, create new module for project "test0_math"
$ cd test0_math
$ mkdir mmath_lib
$ go mod init test0_math
3, add function for module "mmath_lib"
$ cd mmath_lib
$ touch calculate.go
4, add following lines in function mmath_lib/calculate.go
package mmath_lib
func Add(a, b int) int {
return (a + b)
}
func Plus(a, b int) int {
return (a - b)
}
func Multiple(a, b int) int {
return (a * b)
}
func Devide(a, b int) (int, int){
return a/b, a%b
}
5, create main function for project "test0_math"
$ cd ..
$ touch test0.go
6, add following lines in test0.go
package main
import (
"fmt"
"test0_math/mmath_lib"
)
func main() {
fmt.Println("This is test0_math")
fmt.Println(mmath_lib.Add(1,2))
}
7, compile main function for project "test0_math"
$ go build test0.go
you should find new binary file named "test0"
$ ./test0
you shoud get output:
This is test0_math
3