0. Write first

This article assumes that you

  1. Master at least one programming language.
  2. Want to learn Go quickly, but don’t have much time.
  3. Don’t like to read too much written narrative

1. Hello World! The program

The main. Go file

// This is a comment
/* This is a comment */
package main // Each go file must belong to a package
// The main package is special. It defines an executable file, and the following func main() is the entry function to the file

import (// Import additional packages
	"fmt" 
)

func main(a) {// The entry function of the executable fileFMT. Print (" Hello World!" );// Console prints Hello World!
}

Copy the code

(There are more notes on this later.)

2. Program structure

2.1 Naming Rules

2.1.1 keyword

The following 25 keywords cannot be named

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
Copy the code

2.1.2 Predefined Names

Here are 30 predefined names for Go that have their own uses. Although it is possible to revise their definitions, it is not advisable to change them arbitrarily.

Built-in constants: true false iota nil Built-in types: int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex128 complex64 bool byte rune Make len cap new append copy close delete complex real imag panic RecoverCopy the code

2.1.3 Naming conventions are recommended

  1. Let’s use the hump nomenclature
  2. Use small variable names with short life cycles
  3. Use large variable names with long lifetimes, and use hump names

2.2 the statement

Four type declarations

  1. Var (named variable)
  2. Const (named constant)
  3. (Named type)
  4. Func (named function)

Example: This example will be modified later by adding (type)

package main

import "fmt"

const boilingF = 212.0 // declare a constant

func main(a) {// declare a function
	var f = boilingF // Declare a variable
	var c = (f - 32) * 5 / 9
	fmt.Printf("boiling point = %g°F or %g°C\n", f, c)
	// Output:
	// boiling point = 212°F or 100°C
}

Copy the code