时间:2019-11-13 11:44  编辑:admin

观点

Drupal8lang1种内置布局,情势<key,value>,相似Java中的HashMap大概Python中的dict(字典)。个中key是可对照的,没有能为slice,果为slice出有真现对照操纵。别的必要注重1面便是map是援用范例,做为参数存正在副做用。

操纵和例子

怎样创立

利用make,语法格局:make(map[key-type]val-type)

能够正在声明的时分初初化:map[key-type]val-type{key:value, ...}

怎样建改

赋值:name[key]=val

删除: delete(name, key)

怎样会见

曲接利用下标:name[key]

带有校验型: val, ok := name[key], ok是false暗示key对应的值没有存正在

例子:

// Maps are Drupal8's built-in associative data type(sometimes called hashes or dicts in other languages)

package main

import "fmt"

func main() {

  // to create an empty map, use the builtin make: make(map[key-type]val-type)

  m := make(map[string]int)

  // set key/value pairs using typical name[key]=val syntax

  m["k1"] = 7

  m["k2"] = 13

  // Printing a map with e.g. fmt.Println will show all of its key/value pairs.

  fmt.Println("map:", m)

  // Get a value for a key with name[key]

  v1 := m["k1"]

  fmt.Println("v1:", v1)

  // the builtin le returns the numbers of key/value pairs when called on a map

  fmt.Println("len:", len(m))

  // the builtin delete removes key/value pairs from a map

  delete(m, "k2")

  fmt.Println("map:", m)

  /**

  * the optional second return value when getting a value from a map

  * indicates if the key was present in the map. This can be used to dismbiguate between missing keys

  * and keys with zero values like 0 or "". Here we didn't need the value itself, so we ignored it with

  * the blank identifer _.

  **/

  _, prs := m["k2"]

  fmt.Println("prs:", prs)

  if !prs {

    fmt.Println("m[\"k2\"] is not exist.")

  }

  // you can also decalre and initialize a new map in the same line with this syntax

  n := map[string]int{"foo": 1, "bar": 2}

  fmt.Println("map:", n)

}

参考材料

https://golang.谷歌.cn/doc/effective_go.html#maps

https://gobyexample.com/maps

本文去自:简书

感激做者:

检察本文:跟我1起教Drupal8lang:Map

标签: Drupal 中文  

Drupal安全更新

亚艾元软件以Drupal为基础,推动Drupal的中国化、Drupal教程中文化、将Drupal与微信相结合,服务广大的中国Drupal用户。

最近Drupal安全升级频繁,亚艾元提醒Drupal用户,及时升级安全补丁。


业务联系:崔克俊
手机: 13381113196
QQ: 372948992
公司电话:010-62669785

亚艾元象棋谱


亚艾元象棋谱,是亚艾元软件的一个实验项
目,以中国象棋棋谱为实例,用来探索最新
的Drupal网站开发、微信公众号开发、微信
公小程序开发、APP开发相关技术