Golang Basic Knowledge 1

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1.slice and Array

Package Main

Import (
"FMT"
)

Func Main () {
s: = []int{1, 2, 3}
SS: = s[1:]
For I: = range SS {
Ss[i] + =//slice Change the value of the array to change the value in the original array
}
Fmt. PRINTLN (s)
SS = Append (ss, 4) after the value of//append is assigned to the variable SS, the SS does not point to the original array
Fmt. PRINTLN (SS)
For I: = range SS {
Ss[i] + = 10
}
Fmt. PRINTLN (SS)
Fmt. PRINTLN (s)
}

Results

[1 12 13]
[12 13 4]
[22 23 14]
[1 12 13]

2.:= and Var

: = must be used within Func, VAR can generate a global variable

Not declared and declared can be used: =, but not declared cannot be used directly =:

  x, _ := f() //错,x已经声明不需要用 :=

  x, _ = f()   //对, x已经声明可以直接使用 =

  x, y := f()  //对, y没有声明

  x, y = f()   //错, y没有声明,不能直接使用 =

How to use 3.map correctly

使用map,必须使用make生成map,否则不能初始化map的值。
var m make(map[string]int)   //使用make来初始化map
m["b"]; ok { //使用ok来判断遍历的正确是否,不用其他方式
  println(v)

  }


4.strct 的初始化和调用

Defining a struct

Type Student struct {id      intname    stringaddress stringage     int}
1.赋值方式:
var s *student = new (Student) s.id = 101s.name = "Mikle" s.address = "Hongqi South Road" S.age = 18
2.var S1 *student = &student{102, "John", "Nanjing Road", 19}
Both of these are pointer types.package main
type S struct { m string}func f() *S { return &S{"foo"} //A}func main() { p := *f() //B,这里加*是表示返回的是指针类型 *&正好中和,*是获取指针的值。 print(p.m) //print "foo"}

5.传参是传指针还是具体的变量。

如果参数func(p *
interface{}) 则只能传指针类型,
interface{}) 可以传指针和具体变量。
package maintype S struct {}func f(x interface{}) {}func g(x *interface{}) {}func main() {    s := S{}    p := &s    f(s) //A correct    g(s) //B incorrect    f(p) //C correct    g(p) //D incorrect}
6.存指针的map的方式,指针是*int
m := make(map[int]*int)
7.for循环的i,获取临时变量的指针,由于go的闭包属性,

Package Main

Const N = 3

Func Main () {
M: = Make (Map[int]*int)

For I: = 0; i < N; i++ {
M[i] = &i//a
}

For _, V: = range m {
Print (*V)
}
}

Because of the closure relationship, the last value that is referenced is the last. So to change this situation, it is best to re-common a variable within the for loop

package mainconst N = 3func main() {m := make(map[int]*int)for i := 0; i < N; i++ {j := int(i)    //这是一个新的变量,因此不会被后来者覆盖。m[i] = &j}for _, v := range m {print(*v)}}

 8.var 的全局变量定义

The type of the variable must be declared, for example: var s int


 






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.