5-Go language foundation - program structure


  1. Name classification:
    Function name variable name constant name type name statement label package name
  2. Naming rules:
    Unicode letters or underscores start with any number of alphanumeric underscores. Upper and lower case letters are different names.
  3. Package level name:
    Names defined outside the function mate with package level names.
    The function defined outside the function is called package level function, and package level function name is also package level name.
  4. Exportable (external visibility)
    If a package level name is capitalized, other packages can use the name when referencing this package. Usage: package name. Name
    No other name can be accessed by an external package.
  5. Name suggestion
    Hump naming is recommended; QuoteRuneToASCII
    Abbreviations are in pure lowercase or uppercase. htmlEscape HTMLEscape instead of HtmlEscape
    The longer a name is used, the more specific its name should be. Don't be afraid that the name is too long, because it will be replaced at compile time.


25 keywords

package  import 
var func type const  
chan  interface map struct
switch case fallthrough  default 
if  else 
for range  select 
goto  break continue return  go defer   
  • Declare related keywords
    Declaration of package: package
    Package import: Import
    Function declaration: func
    Variable declaration: var
    Type declaration: type
    Constant declaration: const
    Underlying types of constants: number, Boolean, string
package main
import (
	//Import the official fmt package and print
const flag= 1 //Automatically identify the type. The value of a constant expression is evaluated at compile time, not run time. Variable in run time.
type myint  int //Define a value type of its own. The underlying type is int
func main(){
	var num myint//num is of type myint, not int, with an initial value of 0
	var show = "hello world"
  • Type related keywords
    Channel type: chan
    interface type: interface
    Dictionary type: map
    Structure type: struct
    Type later

  • Keywords related to program logic:
    Switch statement: switch case fallthrough default
    If statement: if else
    For statement: for range
    Other: goto break continue return go defer select
    Goto break continue return go defer select

package main
import "fmt"
func main() {
	//In go, switch is equivalent to each case with break at the end,
	//After the match is successful, it will not automatically execute other case s down, but jump out of the whole switch, 
	//But you can use fallthrough to enforce the following case code,
	//fallthrough does not determine whether the expr result of the next case is true.
	var a = 3
    switch a {
    case 1:
    case 2:
    case 3:
            fmt.Println("default case")

	if a==1{
	}else if a==2{

	for i:=0;i<10;i++{
	var arr1=[]string{"aaa","bbb","ccc"}
	for k,v:=range arr1{


Predefined name

Predefined names can be redefined.

Built in constant: true false iota nil
//Built in type: int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex128 complex64
bool byte rune string error
//Built in function: make len cap new append copy close delete
complex real imag
panic recover


  1. Package statement:
  2. Type declaration:
    var const type func
  3. Variable declaration
    var variable name type = expression
    The initial value of a variable is also initialized to zero by default
    Short declaration used inside the function:
    Tuple assignment
    i,j=j,i //Here I'm swapping the values of J
  4. Variable default initialization zero
    The zero value of a variable of numeric type is 0
    The zero value of a variable of boolean type is false
    Zero value of string type is empty string
    The zero value of interface and reference type is nil
    The zero value of array and structure is the zero value of corresponding type of each element and field
    Go language has no uninitialized variables.
  5. Other
    ok return value
246 original articles published, praised 23, visited 150000+
Private letter follow

Tags: Go

Posted on Wed, 15 Jan 2020 05:50:40 -0500 by littlehelp