go常用命令

1、字符串和int之间相互转换

  string转成int:
  int, _ := strconv.Atoi(string)

  string转成int64:
  int64, _:= strconv.ParseInt(string, 10, 64)

  int转成string:
  string := strconv.Itoa(int)

  int64转成string:
  string := strconv.FormatInt(int64,10)

 

2、接口转字符串

      比较粗暴的方法:str := fmt.Sprintf("%v", v)

 

 

     或者:

func Strval(value interface{}) string {
// interface 转 string
var key string
if value == nil {
return key
}

switch value.(type) {
case float64:
ft := value.(float64)
key = strconv.FormatFloat(ft, 'f', -1, 64)
case float32:
ft := value.(float32)
key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
case int:
it := value.(int)
key = strconv.Itoa(it)
case uint:
it := value.(uint)
key = strconv.Itoa(int(it))
case int8:
it := value.(int8)
key = strconv.Itoa(int(it))
case uint8:
it := value.(uint8)
key = strconv.Itoa(int(it))
case int16:
it := value.(int16)
key = strconv.Itoa(int(it))
case uint16:
it := value.(uint16)
key = strconv.Itoa(int(it))
case int32:
it := value.(int32)
key = strconv.Itoa(int(it))
case uint32:
it := value.(uint32)
key = strconv.Itoa(int(it))
case int64:
it := value.(int64)
key = strconv.FormatInt(it, 10)
case uint64:
it := value.(uint64)
key = strconv.FormatUint(it, 10)
case string:
key = value.(string)
case []byte:
key = string(value.([]byte))
default:
newValue, _ := json.Marshal(value)
key = string(newValue)
}

return key
}

3、go定义多行字符串

 4、定义一个map

var num = map[string]int{"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}

5、居然有copy命令和排序命令
func merge(nums1 []int, m int, nums2 []int, n int) {
copy(nums1[m:], nums2)
sort.Ints(nums1)
fmt.Println("======nums1:", nums1)
}

6、str := fmt.Sprintf()用来输出字符串

  Go 可以使用 fmt.Sprintf 来格式化字符串,格式如下:

  s := fmt.Sprintf("%s is %d years old.\n", name, age)

7、针对转义符的处理不同,printf()函数会将输入参数中的%d类型的值进行带入,但是fmt.Println不会

8、数组的range

 for i in range list:

   i是序号

 for index, value in range list:

   index 和value

9、go 输出table

  https://github.com/jedib0t/go-pretty/tree/main/table

10、命令行的入参可以采用os.Args变量;

  命令行的开关可以采用flag

11、go的块注释和c语言的块注释一样

  /*

 */

12 go语言定义变量的时候,支持多变量一起定义:

  var v1 int 

  var v2 int 可以合并:

  var (

   v1 int 

   v2 int 

  )

13 go提供多重赋值功能

  i,j = j, i

  python也支持

 

14、import _ "fmt"

  下划线的意思是说,只调用fmt的init函数,无法使用fmt包中的变量和函数, 所以上述程序错误。

 

15、通过const关键字赋值,可以不用带类型

  const zero = 0.0

 const u float32 = 0 //u=0.0

 

16、 系统默认推导64为int类型

   var value 2 int32

   value1 := 32

   value2 = value1 //编译错误

   value2 = int32(value1) //编译正确

   

  系统默认推导64.0为float64类型

       var value 2 float32

       value1 := 32.0

       value2 = value1 //编译错误

      value2 = float32(value1) //编译正确

 

17、***go里面的数组是值类型,在进行赋值和作为参数传递时,会进行一次复制操作。

        所以作为入参的时候,函数里的值发生改变了,不会影响函数外的值。

 

18、切片

  • 元素个数
  • 存储空间
  • 切片时指向数组的指针

  切片创建的方法:

  1、var mySlice []int=myArray[:5]

  2、mySlice1 := make([]int,5)  mySlice1 := make([]int,10)

  3、mySlice1 := []int{1,2,3,4,5}  

 cap(mySlice)切片分配的空间大小

 len(mySlice)当前所存储的元素个数

 

19 append

  myslice = append(myslice, 1,2,3)

  myslice = append(myslice, myslice1...) 

 

20、copy()介绍

  slice1 := []int{1,2,3,4,5}

  slice2 := []int{5,4,3}

 copy(slice2, slice1) //复制slice1前面三个元素到slice2中

 copy(slice1, slice2) //复制slice2到slice1的前三个元素

 

21、person, ok = personDB["123"]

  if ok {

   //存在

  } else {

   //不存在

  }

22、go map删除

      delete(myMap, "1234")

     1、如果123,不存在,不影响,不报错;

      2、如果myMap时nil,则panic抛出异常;

 

23、go的跳转语句

     goto 

 

24、不定参数的利用

     func myfunc(args ...int){

          for _,arg := range args{

               fmt.Println(arg)

        }

    }

   ...type实际上是一个切片,这样做的好处是省去了定义一个结构[]int{}

 

25、defer是先进后出的原则,有点像堆

 

26、panic和recover

       panic直接报错,退出,例如:panic(404)

       recover用于恢复错误的流程。比如panic后,直接退出,但是采用recover可以实现捕获,然后继续运行。例如:

       def func() {

              if r:=recover();r != nil{

                   log.Printf("Runtime error caught: %v", r)

           }

     }

   涉及到def的传参:https://www.cnblogs.com/nulige/p/10233200.html

27、go可以给任意类型增加相应的方法: 这样处后,a就是一个新的类型,又是整数,有带有方法

        type Interger int

        fun (a Intergert) Less(b Interger) bool{

            return a < b

       }    

     func main() {

       var a Integer = 1 

      if a.Less(2){

        fmt.Println(a, "Less 2")

    }

     }

28go的引用:

 var a = [3]int{1,2,3}

  b := &a

  b[1]++

  fmt.Println(a, *b) 

 

29、go中结构体相当于类,同样会有继承

  type Base struct{

    Name string

  }

 fun (base *Base) Foo(){

  ...

 }

 

30、如果是小写的变量,只能在同一个包级别访问

31、channel的声明

 var ch chan int

 channel的定义:

 ch := make(chan int)

 ch <- value

 value :=<-ch

32、可以用select来进行超时的限制。

33、设置go并发数;

  runtime.GOMAXPROCS(cpu核心数目)

  runtime.NumCPU()获取的是宿主机的核心数

34、go提供了两种锁的类型: sync.Mutex和sync.RWMutex

  典型的用法:

  var l sync.Mutex

 fun fool(){

   l.Lock()

   def l.Unlock()

   //...

  }

35、go的全局只执行一次 sync.Once

36 json字符串转其他类型 以及 其他类型转json字符串

  import "encoding/json"

  json字符串转其他类型

      字符串或者[]bytes转列表、结构体、

  s = `[1, 2, 3, 4]`

  var a []int

  // 将字符串反解析为数组

  json.Unmarshal([]byte(s), &a)

  fmt.Println(a)  // [1 2 3 4]

     type User struct {
    Id int `json:"id"`
    Name string `json:"name"`
     }

    var user User

    // 将字符串反解析为结构体
    json.Unmarshal([]byte(s), &user)
   fmt.Println(user) // {1 wxnacy}

   参考:https://blog.csdn.net/learn8more/article/details/103592844

   其他类型转字符串

    b,err := json.Marsha1(book) 

 37、go并发

 

 

 

 

posted on 2021-02-04 14:45  星星眨着眼  阅读(120)  评论(0编辑  收藏  举报

导航