This is a creation in Article, where the information may have evolved or changed.
[TOC]
Go Language Introduction
2-Basic elements
printf Parameter Memo:
| symbols |
explain |
| %d |
Decimal integer |
| %x,%o,%b |
Integer in hexadecimal, octal, binary |
| %f,%g,%e |
Floating-point number:3.141593, 3.141592653589793, 3.141593e+00 |
| %t |
Boolean:true or False |
| %c |
Rune (Unicode code point) |
| %s |
String |
| %q |
Quoted string "abc" or rune ' C ' |
| %v |
Any value in a natural format |
| %T |
Type of any value |
| %% |
Literal percent sign (no operand) |
Named
Naming rules are similar to C.
GoThere are 25 keywords that cannot be used as a name:
| Break |
Default |
Func |
Interface |
Select |
| Case |
Defer |
Go |
Map |
struct |
| Chan |
Else |
Goto |
Package |
Switch |
| Const |
Type |
If |
Range |
Fallthrough |
| Continue |
For |
Import |
Return |
Var |
GoThere are also 37 pre-declared names for built-in constants, types, and functions (can be overridden):
Constants: true, false, iota, nilTypes: int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex64 complex128 bool byte rune string error Functions: make len cap new append copy close delete complex real imag panic recover
The first character of a name determines its visibility. Uppercase characters indicate public. The package name is always lowercase characters.
The name length is not limited, but tends to shorter names, named using Hump style.
Variable declarations can be sorted by list, such asvar b, f, s = true, 2.3, "four"
Support tuple assignment , such asi, j = j, i // swap values of i and j
A simplified form of declaration must declare at least one new variablevar i, j int; i, j := 1, 2 // compile error: no new variables
The pointer C is similar to the value that takes the * & address, such as*p++
++ec2.go++ (add command line arguments)
package mainimport ( // 导入多个包的常用形式 "fmt" "flag" "strings")var n = flag.Bool("n", false, "omit trailing newline")var sep = flag.String("sep", " ", "separator")func main() { flag.Parse() fmt.Print(strings.Join(flag.Args(), *sep)) if !*n { fmt.Println() }}
Points
newand delete C++ similar, but they are functions
The variable life cycle C is Java consistent with or as the dominant language, and new the allocated object may be in the stack
An implicit assignment such asmedals := []string{"bronze", "silver", "gold"}
typeDeclares a new type, does not change its value, supports operations of its underlying type
Package mechanism similar Java to the path of the package corresponding file
The package also embodies the namespace namespace , similar toPython
The first character capitalization is the entity that is exported (such as variables, functions, types, and so on)
Follow the package declaration as a document description of the package, similar to the Python
The introduction of unused packages can lead to compilation errors
func init() { /* ... */ }is the initialization function of the file/package
Scope ranges are similar to other languages, but Go there are some strange uses for syntax reasons
if f, err := os.Open(fname); err != nil { // compile error: unused: f return err}f.ReadByte() // compile error: undefined f
var cwd stringfunc init () {CWD, err: = OS. GETWD ()//compile ERROR:UNUSED:CWD if err! = Nil {log. Fatalf ("OS. GETWD failed:%v ", Err)}}