Study GO: GO keyword language

GO language Keywords:

	 	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
if 、 else、 break 、 for continue
	for i := 0; i < 10; i++ {
		if i == 2 {
			continue
		}
		if i == 3 {
			break
		}
	}

if else flow control, for circulation, break out of the current cycle, continue this cycle out

swich 、case、 default
 n := 2
	switch n {
	case 1:
		fmt.Println("n = 1")
	case 2:
		fmt.Println("n = 2")
	default:
		fmt.Println(" n不等于1 也不等于2  ")
	}

The case flow control is performed not performed default

func
func method(){
	fmt.Println("我是一个无参无返回值的函数")
}

Defined Functions

interface
type person interface{
		work()
}

Defined interfaces

defer
 	defer	fmt.Println("我后执行")
	fmt.Println("我先执行")

Resources for release adds defer the final implementation of the statement If you have more advanced after the out

where
  var a int
  var b int = 3

Define constants

const
  const a int= 1
  const b = "我是一个常量"
  const(
		i = iota + 1
		ii
		iii
	)

Enumeration constants defined

goto
	for i := 0; i < 10; i++ {
		if i == 2 {
			goto xx
		}
	}

xx:
	fmt.Println("跳转到这里")

Process control jumps to the label label is not recommended

map
	//定义一个hash 未初始化
	var hash1 map[string]int
	
	//使用make 来初始化内存
	var hash2 = make(map[string]int, 5)

Defining a hash (dictionary) data type associated

type 、 struct
type person  struct {
	name string
	age int
}

type person interface{
	engagement()
}

type definition type, struct definition of the structure is similar to the JAVA pojo

chan
 	//为 nil  未初始化
 	var ch1 chan string

    //定义一个容量为0的管道
    var ch2 = make(chan string)

    //定义一个容量为10的管道
    var ch2 = make(chan string10)
 

Features conduit go language may be understood as a message queue FIFO

package 、 import
package main

import "fmt"

defined package bag package incorporated import

fallthrough
	n := 2
	switch n {
	case 1:
		fmt.Println("n = 1")
	case 2:
		fmt.Println("n = 2")
		fallthrough
	default:
		fmt.Println(" n不等于1 也不等于2  ")
	}

Then the output "n = 2" "n is not equal to 1 does not equal 2" fallthrough keywords will enforce the following statement after the establishment of case

range
	//定义一个map
	var hash = make(map[int]int,10)
	//填充数据
	for i:=0 ;i < 10;i++{
		hash[i+i] = i
	}
	//range 遍历
	for k,v := range hash{
		fmt.Println(k,v)
	}

If the map is used to cycle through the type may be obtained directly "key" and "value", if the array is obtained "index" with "value"

return
func method(x, y int) (z int) {
	z = x + y
	return
}

It used to return from a function

select
select {
//如果 从chan中取到数据 则 :
case  <- chan:
	return 1
}

select {
//如果 向chan录入数据 则 :
case  chan <- 1:
	return 2
}

is used to select and monitor operations related duct (channel), it will continue to detect input and output conduit

Released eight original articles · won praise 1 · views 1045

Guess you like

Origin blog.csdn.net/ZHOUAXING/article/details/105005583