Golang learning road-detailed explanation of the use of packages

Leading out of the package

  1. In actual development, we often need to call the defined functions of other files in different files, such as main.go, to use the functions in the utils.go file. How to achieve? ->Package.
  2. Now two programmers are jointly developing a Go project. Programmer A wants to define a function Cal, and programmer B also wants to define a function called Cal. The two programmers quarreled over this. What should I do? ->Package.

Schematic diagram of the package

The essence of the package is actually to create different folders to store program files.

The basic concept of the package

Every file in go belongs to a package, which means that go manages the file and project directory structure in the form of a package.

Three functions of the package

  1. Distinguish identifiers such as functions and variables with the same name.
  2. When there are many program files, the project can be managed well.
  3. Control the access scope of functions, variables, etc., that is, scope.

Description of the package

Basic syntax for packaging

package 包名

The basic syntax of the imported package

import “包的路径”

Quick start for package usage

Package quick start: call functions each other, define func Cal in the file utils.go, put utils.go in a package, when other files need to use utils.go, you can import the package and you can use it .

Code demo:
Insert picture description here
utils.go file

package utils

import(
	"fmt"
)
func Cal(n1 int,n2 int, operator byte) int{
    
    
	var res int
	switch operator {
    
    
	case '+':
		res = n1 + n2
	case '-':
		res = n1 - n2
	case '*':
		res = n1 * n2
	case '/':
		res = n1 / n2	
	default:
		fmt.Println("操作符号错误!")
		
	}
	return res
}

main.go file

package main

import(
	"fmt"
	//导入包,从src下开始引入
	"go_code/day4/utils"
) 

func main()  {
    
    
	var n1 int = 10
	var n2 int = 20
	var operator byte = '+'
	//调用Cal函数
	res := utils.Cal(n1,n2,operator)
	fmt.Println("result =",res)
}

operation result:
Insert picture description here

Precautions for package use

  1. When packaging a file, the package corresponds to a folder. For example, the package name corresponding to the utils folder here is utils. The package name of the file is usually the same as the name of the folder where the file is located, usually in lowercase letters.
  2. When a file needs to use other package functions or variables, the corresponding package needs to be imported first.
    Import method 1: import "package name"
    Import method 2: import (
    "package name 1"
    "package name 2"
    ) The
    package instruction is on the first line of the file, followed by the import instruction.
    When importing a package, the path starts under the src of $GOPATH, without src, the compiler will automatically import it from src.
  3. In order to allow files in other packages to access the functions of this package, the first letter of the function name needs to be capitalized , similar to public in other languages, so that it can be accessed across packages.
  4. When accessing other packet functions, variables, the syntax is the package name. Function name .
  5. If the package name is long, Go supports aliasing the package. Pay attention to the details: after aliasing, the original package name cannot be used.
package main

import(
	"fmt"
	//取别名为util,原来的包名utils不能使用,否则报错,只能使用util
	util "go_code/day4/utils"
) 

func main()  {
    
    
	var n1 int = 10
	var n2 int = 20
	var operator byte = '+'
	//调用Cal函数
	res := util.Cal(n1,n2,operator)
	fmt.Println("result =",res)
}
  1. In the same package, there cannot be the same function name, nor the same global variable name, otherwise it will report a duplicate definition error.
  2. If you want to compile into an executable program file, you need to declare the package as main, which is package main. This is a grammar specification. If you are writing a library, the package name can be customized.

Guess you like

Origin blog.csdn.net/weixin_44736475/article/details/113921956