SoFunction
Updated on 2025-03-01

Basic knowledge of GO literacy precautions

1.GO program directory structure

existGOPATHStructure under the directory

--bin(Stores the compiled executable text)
|----(Executable file)
--pkg(Store the compiled package text)
|-----(后面用到了exist说)
--src(Store project sources)
|----hello(Project Catalog)
|------(Entry file,Here's a detailed explanation)
|----...(Multiple directories and abovehelloThe same form)
|-------...

2. General structure of GO program

Before introducing it, I will review the previous file structure

package main
import (
    "fmt"
)
func main() {
    ("Hello World")
}

The Go program is throughpackageIt is organized similar to python, onlypackageA package named main can contain a main function, an executable program.There are and onlyA main package

passimportKeywords to import other non-main packages

passconstKeywords to define constants

passvarKeywords to declare and assign global variables

passtypeKeywords to declare structure or interface

passfuncKeywords to declare functions

3. Import and use of GO packages

Import of packages

Introduce various packages separately

import "fmt"
import "io"
import "time"
import "strings"

Unified introduction, eliminating multiple import keywords

import(
    "fmt"
    "io"
    "time"
    "strings"
)

If the imported package is not called, a compilation error will be reported during compilation. This is very critical. Therefore, when compiling an error, you can check whether the imported package has not been called.

Alias ​​and application of packages

In actual work, in some special cases such as abbreviation or unified naming.

import(
    print "fmt"
)

The above is to replace fmt with print, before it is replaced:

("Hello World")

After replacement:

("Hello World")

There is also an omitted call to the package, which is very unintentional, so I won't introduce it.

4. Visibility specifications (very important)

Use case in GO language to determine whether the constant, variable, type, interface, structure, or function can be called by an external package.

The first letter of the function name is private in lowercase, indicating privateness and cannot be called externally

The first letter of the function name is capitalized as public, indicating public, and can be called externally

1. Example of the first letter lowercase function: (cannot be called by the name part)

func getDate(){
    return ...
}

2. First letter capitalization function example: (can be called externally)

func GetDate(){
    return ...
}

5. GO built-in keywords

There are 25 commonly used, all lowercase

break
default
func
interface
select
case
defer
go
map
struct
chan
else
goto
package
switch
const
fallthrough
if
range
type
continue
for
import
return
var

6. GO comment method

//: Indicates a single line comment/* */   : Indicates multiple lines of comment

This article is mainly a literate text on the basic knowledge of GO language, which mainly explains the GO program directory structure, the import and alias of GO program packages, the built-in keywords of GO, the GO annotation method and other related knowledge. For more basic knowledge of GO language, please check the relevant links below.