Golang中map的三种声明方式和简单实现增删改查
package main import (
"fmt"
) func main() {
test3 := map[string]string{
"one": "php",
"two": "golang",
"three": "java",
}
no_exsits, ok := test3["three"]
fmt.Println(no_exsits, ok)
if no_exsits, ok := test3["four"]; ok {
fmt.Println(no_exsits)
} else {
fmt.Println("KEY not exist")
} } //java true
//KEY not exist
map就相当于php语言中的关联数组
//代码
package main
import (
"fmt"
) func main() {
//第一种声明
var test1 map[string]string
//在使用map前,需要先make,make的作用就是给map分配数据空间
test1 = make(map[string]string, )
test1["one"] = "php"
test1["two"] = "golang"
test1["three"] = "java"
fmt.Println(test1) //map[two:golang three:java one:php] //第二种声明
test2 := make(map[string]string)
test2["one"] = "php"
test2["two"] = "golang"
test2["three"] = "java"
fmt.Println(test2) //map[one:php two:golang three:java] //第三种声明
test3 := map[string]string{
"one" : "php",
"two" : "golang",
"three" : "java",
}
fmt.Println(test3) //map[one:php two:golang three:java] language := make(map[string]map[string]string)
language["php"] = make(map[string]string, )
language["php"]["id"] = ""
language["php"]["desc"] = "php是世界上最美的语言"
language["golang"] = make(map[string]string, )
language["golang"]["id"] = ""
language["golang"]["desc"] = "golang抗并发非常good" fmt.Println(language) //map[php:map[id:1 desc:php是世界上最美的语言] golang:map[id:2 desc:golang抗并发非常good]] //增删改查
// val, key := language["php"] //查找是否有php这个子元素
// if key {
// fmt.Printf("%v", val)
// } else {
// fmt.Printf("no");
// } //language["php"]["id"] = "3" //修改了php子元素的id值
//language["php"]["nickname"] = "啪啪啪" //增加php元素里的nickname值
//delete(language, "php") //删除了php子元素
fmt.Println(language)
}
补充:判断一个不存在的key
package main import (
"fmt"
) func main() {
test3 := map[string]string{
"one": "php",
"two": "golang",
"three": "java",
}
no_exsits, ok := test3["three"]
fmt.Println(no_exsits, ok)
if no_exsits, ok := test3["four"]; ok {
fmt.Println(no_exsits)
} else {
fmt.Println("KEY not exist")
} }
//java true
//KEY not exist
Golang中map的三种声明方式和简单实现增删改查的更多相关文章
- springboot(三 使用mybatis +springboot 完成简单的增删改查)
先说一些注解: @EnableAutoConfiguration 可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器 ...
- ElasticSearch6(三)-- Java API实现简单的增删改查
基于ElasticSearch6.2.4, Java API创建索引.查询.修改.删除,pom依赖和获取es连接 可查看此文章. package com.xsjt.learn; import java ...
- 数组的三种声明方式总结、多维数组的遍历、Arrays类的常用方法总结
1. 数组的三种声明方式 public class WhatEver { public static void main(String[] args) { //第一种 例: String[] test ...
- 用CI框架向数据库中实现简单的增删改查
以下代码基于CodeIgniter_2.1.3版 用PHP向数据库中实现简单的增删改查(纯代码)请戳 http://www.cnblogs.com/corvoh/p/4641476.html Code ...
- 细说java中Map的两种迭代方式
曾经对java中迭代方式总是迷迷糊糊的,今天总算弄懂了.特意的总结了一下.基本是算是理解透彻了. 1.再说Map之前先说下Iterator: Iterator主要用于遍历(即迭代訪问)Collecti ...
- Java中Map的三种遍历方法
Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...
- Map的三种遍历方式
对于Map的三种方式遍历 1.keySet() 2.values() 3.entrySet()三种方式得到Set之后,都可以使用 foreach或者iterator, 不能使用for,因为数据结构决定 ...
- 上手spring boot项目(三)之spring boot整合mybatis进行增删改查的三种方式。
1.引入依赖. <!--springboot的web起步依赖--><dependency> <groupId>org.springframework.boot< ...
- 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)
我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...
随机推荐
- 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)
Implement atoi which converts a string to an integer.The function first discards as many whitespace ...
- Case when then esle end
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...
- Python Anaconda使用
选择Python 科学计算器发行版 Python用于科学计算的一些常用工具和库 IPython-增强的交互环境:支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函 ...
- vue路由(一个包含重定向、嵌套路由、懒加载的main.js如下)and 路由跳转传参的query和params的异同
import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'Vue.use(VueRouter)cons ...
- Oracle如何查询当前的crs/has自启动状态
我们知道在某些停机测试场景,是需要人为禁用crs/has的自启动的,防止过程中主机反复重启对数据库集群造成影响. 使用crsctl disable/enable crs命令可以禁用/启用crs的自启动 ...
- scrapy yield
生成器 一个带有 yield 的函数就是一个 generator,它和普通函数不同,生成一个 generator 看起来像函数调用,但不会执行任何函数代码,直到对其调用 next()(在 for 循环 ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- SQL性能优化前期准备-清除缓存、开启IO统计
文章来至:https://www.cnblogs.com/Ren_Lei/p/5669662.html 如果需要进行SQl Server下的SQL性能优化,需要准备以下内容: 一.SQL查询分析器设置 ...
- tcp_协议基础
具体7层 数据格式 功能与连接方式 典型设备 应用层 Application 数据Data 网络服务与使用者应用程序间的一个接口 终端设备(PC.手机.平板等) 表示层 Presentation ...
- tr字符串的梗
工作问题总结 声明:应用程序代码为前人所写,仅仅只是维护代码,暂且不论代码风格瑕疵. 结论: 使用tr“包”起来的字符串,可能会被翻译为不同的语言,此时,应用程序在不同语言环境下可能会表现BUG. 1 ...