zoukankan      html  css  js  c++  java
  • go内建方法 new和make区别

    package main

    import (
    "fmt"
    "reflect"
    )

    func main() {

    // make函数
    //makeSlice() // 创建切片
    //makeMap() // 创建集合
    //makeChan() // 创建channel
    NewMap() // make创建的是指针类型 new的是引用类型
    }

    func NewMap() {
    mp := new(map[int] string)
    mp1 := make(map[int] string)
    fmt.Println(reflect.TypeOf(mp))
    fmt.Println(reflect.TypeOf(mp1))
    }


    func makeSlice(){
    sl := make([]string,3)
    sl[0] = "a";
    sl[1] = "b";
    sl[2] = "c";
    fmt.Println(sl)

    }

    func makeMap(){
    mp := make(map[int] string)

    mp[0] = "hello"
    mp[1] = "world"
    mp[33] = "!"
    fmt.Println(mp)
    }

    func makeChan() {
    mchan := make(chan string)

    go func() {
    mchan <- "hello world"
    }()

    message := <- mchan

    fmt.Println(message)
    }
  • 相关阅读:
    Ugly Numbers
    Present
    Out of Hay
    Stars(树状数组或线段树)
    Humble Numbers
    Wireless Network
    Rank List
    Argus
    食物链
    Antenna Placement
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13056202.html
Copyright © 2011-2022 走看看