加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

go语言结构体定义使用

发布时间:2020-12-16 18:42:53 所属栏目:大数据 来源:网络整理
导读:package mainimport ( "fmt" "sync")type Info struct { info map[int]string mu sync.RWMutex}func main() { x := Info{info: make(map[int]string)} x.Set(1,"golang") s := x.Get(1) fmt.Println(s)}func (s *Info) Get(i int) string { s.mu.RLock() inf
package main

import (

    "fmt"

    "sync"

)

type Info struct {

    info map[int]string

    mu   sync.RWMutex

}

func main() {

    x := &Info{info: make(map[int]string)}

    x.Set(1,"golang")

    s := x.Get(1)

    fmt.Println(s)

}

func (s *Info) Get(i int) string {

    s.mu.RLock()

    info := s.info[i]

    s.mu.RUnlock()

    return info

}

func (s *Info) Set(i int,name string) bool {

    s.mu.Lock()

    defer s.mu.Unlock()

    _,present := s.info[i]

    if present {

        return false

    }

    s.info[i] = name

    return true

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读