go-program structure

Declaration package

Both java and go are used packageto declare packages. The purpose of using package in java is to avoid naming conflicts, implement access control, and provide class location and search ; for example, use package to declare in java files one.java and two.java. The path of your own package is used to provide a unique access path to other places where this class is called;

In java, the package has access permissions, and a certain call can only access the classes in the package when it has the permission to access a package

In go, the package declaration still usespackage

Import package

At the same time, take java as an example. Suppose there are two files. Use one.java importto locate the package declared by two.java to search for the location of two.java, so as to achieve the purpose of operating the methods and attributes in two.java. .

In java, you need to write the import statement that introduces the package before the package statement that declares the package

In python, the introduction and declaration of packages are different from those in java. In python, only when __init__.py files exist in a folder will it be considered as a python package; in python, use import/from ... import ...to introduce a certain need to be Call a class of a package or a method of a class (location path)

In go, the introduction of packages is still used import, which is similar to the usage habits of java; to compare the import of go and python, the use of import in go introduces packages , and it needs to be added when importing a certain package ""; python introduces classes/ Modules/methods/variables, etc.;

Format and print "hello" characters

package main
// 导入fmt包,fmt是一个包!
import "fmt"
func print_hello(){
    
    
    fmt.Print("hello")
}

go run 和 go build

go run runs the source code program
go build to generate a binary executable file


package main

Every go application contains a package named main;

Uppercase and lowercase letters (permission issue)

When the identifier in a package starts with a capital letter, the identifier can be used by the caller who introduced the package ( public); when the identifier in a package starts with a lowercase letter, the identifier is not visible outside the package Is visible and available in the package ( protected);

The opening brace cannot be on its own line

In go, when defining a function, the left curly brace cannot be on its own line, otherwise an error will be reported; (perhaps the willfulness of the obsessive-compulsive developer)

Guess you like

Origin blog.csdn.net/qq_39378657/article/details/112642179