“Mapa de Golang” Respostas de código

Inicializar mapa em Golang

// By default maps in Go behaves like a default dictionary in python
m := make(map[string]int)

m["Dio"] = 3
m["Jonathan"] = 1
Joseph Joestar

vá remover do mapa

m := map[string]string{"key1": "val1", "key2": "val2"}
delete(m, "key1")
Navid2zp

vá adicionar ao mapa

m := make(map[string]int)
m["numberOne"] = 1
m["numberTwo"] = 2
Navid2zp

Mapa de Golang

package main
import "fmt"

func main() {

  object := map[string]string{
    "name": "john doe",
  }
  
  arrayObject := []map[string]string{
    map[string]string{ "name": "john doe"},
    map[string]string{"name": "jane doe"},
  }

  usingMake := make(map[string] string)
  usingMake["name"] = "max cavalera"
  
  fmt.Printf("this is object %v \n", object)
  fmt.Printf("this is array object %v \n", arrayObject)
  fmt.Printf("using make method %v \n", usingMake)
}
Restu Wahyu Saputra

Mapas de Golang

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type Address struct {
	Street  string `json:"street"`
	Suite   string `json:"suite"`
	Zipcode string `json:"zipcode"`
}

type Users struct {
	Name string `json:"name"`
	Age  uint   `json:"age"`
}

func Mapping() {
   // users 1 example
	var users1 map[string]interface{}

	users1 = map[string]interface{}{"name": "john doe", "age": 23}

   // users 2 example
	users2 := map[string]interface{}{"name": "john doe", "age": 23}

   // users 3 example
	users3 := make(map[string]interface{})
	users3 = map[string]interface{}{"name": "John doe", "age": 23}

   // users 4 example
	var users4 map[string]interface{} = map[string]interface{}{"name": "John doe", "age": 23}

	fmt.Printf("Object mapping 1 %#v \n", users1)
	fmt.Printf("Object mapping 2 %#v \n", users2)
	fmt.Printf("Object mapping 3 %#v \n", users3)
	fmt.Printf("Object mapping 4 %#v \n", users4)
}

func ArrayMapping() {
   // users 1 example
	var users1 []map[string]interface{}

	users1 = []map[string]interface{}{
		map[string]interface{}{"name": "john doe", "age": 23},
	}

   // users 2 example
	users2 := []map[string]interface{}{
		map[string]interface{}{"name": "john doe", "age": 23},
	}

   // users 2 example
	users3 := make([]map[string]interface{}, 1)
	users3 = []map[string]interface{}{
		map[string]interface{}{"name": "John doe", "age": 23},
	}

   // users 4 example
	var users4 []map[string]interface{} = []map[string]interface{}{map[string]interface{}{"name": "John doe", "age": 23}}

	fmt.Printf("Array object mapping 1 %#v \n", users1)
	fmt.Printf("Array object mapping 2 %#v \n", users2)
	fmt.Printf("Array object mapping 3 %#v \n", users3)
	fmt.Printf("Array object mapping 4 %#v \n", users4)
}

func main() {
	Mapping()
	fmt.Printf("\n")
	ArrayMapping()
}
Restu Wahyu Saputra

Mapa de Golang

myMap := make(map[(key type)](value type))
Itchy Ibex

Respostas semelhantes a “Mapa de Golang”

Perguntas semelhantes a “Mapa de Golang”

Mais respostas relacionadas para “Mapa de Golang” em Go

Procure respostas de código populares por idioma

Procurar outros idiomas de código