Implement addition, subtraction, multiplication and division operations
Traditional method implementation:
var n1 float64 = 1.2 var n2 float64 = 2.1 var opt byte = '+' var res float64 switch opt { case '+': res = n1 + n2 case '-': res = n1 - n2 case '*': res = n1 * n2 case '/': res = n1 / n2 default: ("Illegal operator") } ("res=%v", res)
Problems with the above code:
- Requirements change, model acquisition, code is not flexible enough
- Not conducive to maintenance
- Code redundancy
Functions can solve this problem
- Package
- Pass the parameter call
- Return value
Basic function syntax
func function name (formal parameter list) (return value list) {
Execution statement
return return list of values
}
The above code is rewritten:
func calc(n1 float64, n2 float64, opt byte) float64 { var res float64 switch opt { case '+': res = n1 + n2 case '-': res = n1 - n2 case '*': res = n1 * n2 case '/': res = n1 / n2 default: ("Illegal operator") } return res } func main() { ("hello fn") var n1 float64 = 1.2 var n2 float64 = 2.1 var opt byte = '+' var result float64 = calc(n1, n2, opt) ("%T %v \n", result, result) }
Function problem
There are so many functions in a file
Files are specifically used to define functions, allowing other files to be called, encapsulation of tool functions,
File, specifically defines functions for database operations
File, introduce other files, use tool functions, and focus more on the code
Then the concept of packages is introduced, and the use of categories is also conducive to collaborative development by multiple people. The same function name can exist under different packages.
Introduction to the principle of the package
The essence of a package is actually to create different folders to store program files.
Each file of go belongs to a package, which means that go manages file and project directory structure in the form of a package.
Identifier starting with capital letters means that the identifier can be exported
effect:
- Distinguish between functions, variables and other identifiers with the same name
- When there are many program files, it is very good to manage projects
- Control the access scope of functions and variables, that is, scope
grammar
package util
Introduce and use (encapsulate tool functions that implement addition, subtraction, multiplication and division)
Package name. Function name
OmittedGOPATH/src
orGOROOT/src
, the premise isenv.GO111MODULE=off
In the case of
import "Path to package" // GOPATH or GOROOT src, env.GO111MODULE=off
utils/ File
package utils import "fmt" // Identifier capitalizes, indicating that it can be exportedfunc Calc(n1 float64, n2 float64, opt byte) float64 { var res float64 switch opt { case '+': res = n1 + n2 case '-': res = n1 - n2 case '*': res = n1 * n2 case '/': res = n1 / n2 default: ("Illegal operator") } return res }
- main/ file
import ( "fmt" "go_code/fndemo1/utils" ) func main() { var n1 float64 = 1.233 var n2 float64 = 2.111 var opt byte = '+' var result float64 = (n1, n2, opt) ("%T %.2f= \n", result, result) } // %.2f Default width, precision 2// float64 3.34
Things to note about the package
The package name is recommended to be consistent with the directory name, or it may be inconsistent.
import "package name"
This package name is the package name defined by package "package name", which has nothing to do with the directory name. If the name defined by package is inconsistent with the directory name, when introducing a package, the package name defined by package should be used as the basis.
- package "package name" must be on the first line of the file and then import
- The packet lead path, under the GOPATH or GOROOT src directory,
env.GO111MODULE
- The function to be exposed in the package must be capitalized to indicate that it can be exported.
- If the package name is relatively long, go supports aliasing. After defining the alias, the original package name will not work well.
- Under the same package, the same function name is not allowed
- If you want to compile into an executable program file, you need to declare this package as main
Enter the project root directory (the execution environment of go build is the directory where the command is run),
go build go_code/fndemo1/main
, will be generated in the root directoryExecutable file
go build -o bin/ go_code/fndemo1/main
, -o
The directory indicating the output, if not, it will be automatically created.
import ( "fmt" util "go_code/fndemo1/utils" ) // use()
The above is the detailed content of learning the use of the basic function package of Go. For more information about the Go function package, please pay attention to my other related articles!