“Idź mapa” Kod odpowiedzi

idź Dodaj do mapy

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

Mapa 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

Mapy 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

Idź z mapy

var id string
var ok bool
if x, found := res["strID"]; found {
     if id, ok = x.(string); !ok {
        //do whatever you want to handle errors - this means this wasn't a string
     }
} else {
   //handle error - the map didn't contain this key
}
Average Angelfish

Idź mapy

type Vertex struct {
	Lat, Long float64
}

var m map[string]Vertex

func main() {
	m = make(map[string]Vertex)
	m["Bell Labs"] = Vertex{
		40.68433, -74.39967,
	}
	fmt.Println(m["Bell Labs"])
}
DevLorenzo

Idź mapa

subjectMarks := map[string]float32{"Golang": 85, "Java": 80, "Python": 81}
SAMER SAEID

Odpowiedzi podobne do “Idź mapa”

Pytania podobne do “Idź mapa”

Więcej pokrewnych odpowiedzi na “Idź mapa” w Go

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu