go (a) the basics

 

 

First, the first program

The basic program structure

package main // follicles

Import " FMT "  // introducing the dependent code

// function realization
func main() { fmt.Println("hello world!") }

important point:

1. Application inlet

  • It must be the main package: Package Penalty for main
  • It must be the main method: func main ()
  • File name is not necessarily main.go

2. Quit return value

The main difference with other programming languages

  • Go in the main function does not support any return value
  • To return status os.Exit

3. Obtain command line arguments

main function does not support passing parameters

func main()

Get command line arguments in the program directly through os.Args

 

 

 

Write test file

Source files _test end: xxx_test.go

Test method names start with Test: func TestXXX (t * testing.T) {...}

package try_test

import "testing"

func TestFirstTry(t *testing.T)  {

    t.Log("My first try!")

}

result

=== RUN   TestFirstTry
--- PASS: TestFirstTry (0.00s)
    first_test.go:7: My first try!
PASS

 

 

Fibonacci sequence

package try

import (
    "fmt"
    "testing"
)

func TestFibList(t *testing.T)  {
    var a int = 1 
    var b int = 1

    // var {
     // omitted int, type inference
     //     A = int. 1
     //     B = int. 1
     // }
     // type inference, of the first test
     // A: =. 1
     // B: =. 1 
    FMT . Print ( A, "  " )
     for I: = 0 ; I < . 5 ; I ++ {
         FMT . Print ( "  " , B)
        tmp:=a
        a=b
        b=tmp+a
    }
    fmt.Println()
}

 

 

Second, variables, constants, data types and operators

Variable assignment

The main difference with other programming languages

Assignments can be automatically inferred

Can be assigned to multiple variables simultaneously in an assignment statement

func TestExchage(t *testing.T){
    a := 1
    b := 1
    //tmp := a
    //a = b
    //b = tmp
    a, b= b, a
    t.Log(a, b)
}

 

 

Constant Defined

The main difference with other programming languages

Quick set a continuous value

const (
    Monday = iota + 1
    Tuesday
    Wednesday
    Thrusday
    Friday
    Saturday
    Sunday
)

const (
    Open = 1 << iota
    Close
    Pending
) 

example

package _const

import "testing"

const (
    Monday = iota + 1
    Tuesday
    Wednesday
)

const(
    Readable = 1<<iota
    Writable
    Executable
)

func TestConstantTry(t *testing.T)  {
    t.Log(Monday, Tuesday, Wednesday)
} // 1,2,3

func TestConstantTry1(t *testing.T)  {
    a := 7 // 0111
    t.Log(a&Readable==Readable, a&Writable==Writable, a&Executable==Executable)
} // true,true,true

 

 

type of data

Basic data types

 

 

The type of conversion

The main difference with other programming languages

1.Go language does not allow implicit type conversion

2. alias can not be performed and the type of the original implicit type conversion

package type_test

import "testing"

type MyInt int64

func TestImplict(t *testing.T)  {
    var a int = 1
    where b int64
    // B = A // do not support implicit type conversion 
    B = Int64 (A)
    t.Log(a, b)
    var c MyInt
    // C = B // alias does not support an implicit conversion 
    C = MyInt (B)
    t.Log(a, b, c)
}

 

Type predefined value

 

 

Pointer types

The main difference with other programming languages

1 does not support pointer arithmetic

func TestPoint(t *testing.T)  {
    a := 1
    aPtr := &a
    t.Log(a, aPtr)
    t.Logf ( " % T% T " , A, aPtr)
     // aPtr = aPtr +. 1 // error pointer arithmetic is not supported 
}


result

=== RUN TestPoint
--- PASS: TestPoint (0.00s)
type_test.go:22: 1 0xc04205e290
type_test.go:23: int *int
PASS

2.string value type, the first test of default is an empty string, rather than nil

func TestString(t *testing.T)  {
    var s string
    t.Log("*"+s+"*")
    t.Log(len(s))
}

result
=== RUN   TestString
--- PASS: TestString (0.00s)
    type_test.go:29: **
    type_test.go:30: 0
PASS

 

 

 

 

Operators

Arithmetic Operators

 

 

Comparison Operators

== compares with an array

  • The same dimensions and containing the same number of elements the array can compare
  • Each element of the same are equal

 

 

Logical Operators

 

 

 

Bitwise Operators

The main difference with other programming languages

 

 

 

Third, write a structured program

cycle

And other major programming language differences

 

 Examples

 

 

Examples

func TestWhileLoop(t *testing.T)  {
    n := 0
    for n < 5{
        t.Log(n)
        n++
    }
}


result
=== RUN   TestWhileLoop
--- PASS: TestWhileLoop (0.00s)
    type_test.go:36: 0
    type_test.go:36: 1
    type_test.go:36: 2
    type_test.go:36: 3
    type_test.go:36: 4
PASS

 

if conditions

 

 Differences with other programming languages

  • condition expression must evaluate to a Boolean value
  • Support for variable assignment
if var declaration; condition {
    // code to be executed if condition is true
}
func TestIfMultiSec(t *testing.T)  {
    if a := 1 == 1;a {
        t.Log("1==1")
    }
}

// A is true, execute the contents of the brackets


result
=== RUN   TestIfMultiSec
--- PASS: TestIfMultiSec (0.00s)
    type_test.go:43: 1==1
PASS

 

 switch condition

 

 The main difference with other programming languages

  • Conditional expression is not limited to constant or integer
  • A single case, the result may have multiple options, separated by commas
  • And C and other rules Conversely, Go language do not need to break a case with a clear exit
  • Conditional expressions may not be set after the switch, in this case, the overall switch structure with a plurality of logic if ... else ... and is equivalent

 

There can be multiple options

func TestSwitchMultiCase(t *testing.T)  {
    for i := 0; i < 5; i++{
        switch i {
        case 0, 2:
            t.Log("Even")
        case 1,3:
            t.Log("Odd")
        default:
            t.Log("It is not 0-3")
        }
    }
}

Use the equivalent of if ... else ...

func TestSwitchCaseCondition(t *testing.T)  {
    for i := 0; i < 5; i++ {
        switch  {
        case i%2 == 0:
            t.Log("Even")
        case i%2 == 1:
            t.Log("Odd")
        default:
            t.Log("unknow")
        }
    }
}

 

Fourth, arrays and slices

 Array Declaration

 

 

 Traversing the array elements

The main difference with other programming languages

 

 

Array interception

 

 

Slice the internal structure

 

 

 

Slice statement

 

 

Sliced ​​shared memory architecture

 

 

 

Array vs sliced

  • Whether scalable capacity
  • Can be compared

 

 

 

 

Five, Map

 

 Map statement

 

 

Map element access

The main difference with other programming languages

 

 

Map traversal

 

 

 Map and factory pattern

  • Map of value can be a way
  • Together, we can easily achieve a single object with the method of the Dock type Go Interface Mode Factory

 

 

Realize Set

 

 

 

 

 

Six String

 The main difference with other programming languages

 

 

 

 Unicode UTF8

 

 

Encoding and storage

 

 

 

Common String Functions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/aidata/p/11729979.html
Recommended