golang1

A variable

go declare variables using the var keyword general form:

var name type

Declare variables on the type of a variable name of the variable after:

var a, b * int

Basic types of go:

  bool, string, int, int8 ... uint ... byte // uint8 alias, rune // int32 alias represent a Unicode code, float32, float64, complex types complex64,128

When a variable is declared, it is automatically given the value of zero type, int is 0, float to 0.0, bool is false, string is an empty string, the pointer is nil.

All memory in go say after initialization.

Naming hump, as numShips

 

standard format:

  var variable name variable type

Batch statement:

var ( 
    A int  
    B String 
    C [] float32 
    // FUNC defined function 
    D FUNC () BOOL 
    // struct custom type 
    E struct { 
        X int 
    } 
)

Short format:

Name: = expression

  1. Define the variables

  2. The data type can not be provided

  3. can only be used inside the function

i,j := 0,1

Variable initialization:

var HP int = 100 // var = variable name type expression or var hp = 100

net.Dial provide specified address and initiates a network connection protocol, the function returns two values, one is the connection object, said one error object:

package main

import "net"

var conn net.Conn
var err error

func main() {
    conn, err = net.Dial("tcp", "127.0.0.1:8080")
}

Multiple assignment in error handling and the return value of the function go language will be widely used:

// The IntSlice declared as type int 
[] type IntSlice int 
// written as a len IntSlice type method, there is provided slicing length 
FUNC (P IntSlice) Len () int            { return len (P)}
 // The supplied i, j comparing element index, the comparison result returned 
FUNC (P IntSlice) Less (I, J int ) BOOL { return P [I] < P [J]}
 // switching element values of two 
func (p IntSlice) Swap (i , J int ) {P [I], P [J] = P [J], P [I]}

Anonymous variables: Underline __

func GetData() (int, int) {
    return 100, 200
}
func main(){
    a, _ := GetData()
    _, b := GetData()
    fmt.Println(a, b)
}

A variable, constant, or the type of function has a role in the range of the program, called the scope

  Local variables defined function is called

  Outside the function defined is called Global Variables

  Defined in the function of the variable called formal parameters

// global variable a 
var a int = 13 is 

FUNC main () { 
    // local variables a and B 
    var a int = . 3 
    var B int = . 4 

    fmt.Printf ( " main () function D% = a \ n- " , A) 
    fmt.Printf ( " main () function D% = B \ n- " , B) 
    C: = SUM (A, B) 
    fmt.Printf ( " main () function D% = C \ n- " , C ) 
} 
// formal parameters 
FUNC SUM (A, B int ) int {
    fmt.Printf ( " SUM () function D% = A \ n- " , A) 
    fmt.Printf ( " SUM () function D% = B \ n- " , B) 
    NUM: = A + B
     return NUM 
}

Signed integral type int, unsigned integral type uint, uintptr

complex128 (64-bit real and imaginary) and complex64 (32-bit real and imaginary)

Values ​​of three-part complex RE (real part) + IMI (IM imaginary part and an imaginary unit I), the former two types are float

Disclaimer complex syntax:

var name = complex128 Complex (X, y) 
// name to the variable name complex, complex128 the type of complex, x, y values of the two types of the plurality of float64 represent, x is the real part, y is the imaginary part
/ / or: name: = complex (x, y)

Use Real () to obtain a real part of the complex number x, using the imag () to obtain an imaginary part of the complex number y

 

Sine sin:

main Package 

Import ( 
    " Image " 
    " Image / Color " 
    " Image / PNG " 
    " log " 
    " Math " 
    " OS " 
) 

FUNC main () { 

    // Image size 
    const size = 300 
    // The given size to create grayscale 
    PIC: = image.NewGray (image.Rect ( 0 , 0 , size, size)) 

    // iterate through each pixel 
    for X: = 0 ; X <size; X ++ {
         for Y: = 0 ; Y <size;and ++ {
            // filled with white 
            pic.SetGray (x, Y, color.Gray { 255 }) 
        } 
    } 

    // generated from 0 to the maximum x coordinate of the pixel 
    for x: = 0 ; x <size; x ++ { 

        // make the value sin ranges between 2Pi ~ 0 
        S: float64 = (X) * 2 * Math.PI / size 

        // amplitude of sin is half a pixel. Half of the pixels shifted down and inverted 
        Y: size = / 2 - math.Sin (S) * size / 2 

        // Draw sin trajectory black 
        pic.SetGray (X, int (Y), color.Gray { 0 }) 
    } 

    // create the file 
    file, ERR: = os.Create ( "sin.png " ) 

    IF ERR! = nil { 
        log.Fatal (ERR) 
    } 
    // write data to a file using the png format 
    png.Encode (file, PIC) // the image information is written to a file 

    // close file 
    file .close () 
}

    Use image package NewGray () function creates a picture object, by using the area image.Rect structure, a square image.Rect described two anchor points (x1, y1) and (x2, y2), image.Rect (0 , 0, size, size) complete indication grayscale, width 300 length 300.

bool type :(! true == false) == True

// If b is true, btoi returns 1 if false, btoi returns 0 
FUNC btoi (b BOOL ) int { IF b { return 1 } return 0 }

 

Type is a string value, immutable.

  Contents of the string (bytes pure) may be obtained by the index, the index is written in [], the index starts from 0

  The first byte of string str (neat bytes): str [0]

  Using the string concatenation operator "+"

  strings.Index: Forward substring search.

  strings.LastIndex: Reverse search substring.

  • Determining whether a letter: unicode.IsLetter (ch)
  • Determining whether the digital: unicode.IsDigit (ch)
  • Determining whether blank symbol: unicode.IsSpace (ch)

Pointer (pointer) in the Go language can be split into two core concepts:

  • Pointer type, allowing the pointer type data modification, data can be transmitted directly using a pointer, without copying the data, not the type of pointer and offset arithmetic.
  • Slice, the pointer points to the start of the original element, the number and capacity element composition.

Pointer default value nil, often abbreviated as the pointer variable ptr

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/xuezhihao/p/11978326.html