Workspace
  • Introduction
  • Algorithm
    • 二叉树
    • 排序算法
  • Basic Knowledge
    • CAP定理
    • CAS-SSO-单点登陆
    • 单向认证-双向认证
  • CICD
  • Cloud Native
  • Docker
    • Docker特性
    • Docker资源隔离
  • Golang
    • Standard Library
      • Archive
        • Builtin
        • Zip
    • Golang-container包
    • Golang-fallthrough关键字
    • Golang For Slect
    • Golang-Goroutine泄露
    • Golang Interface
    • Golang-json.Unmarshal
    • Golang Label
    • Golang Map String Struct
    • Golang Map To Struct
    • Golang Override Package Function
    • Golang-Slice删除元素
    • Golang Switch
    • Golang-sync.Cond
    • Golang-sync.Map
    • Golang-sync.once
    • Golang-type关键字
    • Golang-代码生成
    • golang-并发数控制
    • Golang-并发退出
    • Golang-插件系统
    • Golang-继承
    • Golang之channel
    • Golang之continue
    • Golang之make与new和nil
    • Golang之map
    • Golang之reflect
    • Golang之类型判断
    • Golang代码质量检测
    • Golang变量避坑
    • Golang字符串遍历
    • golang并发控制代码示例
    • Golang性能优化
    • Golang死锁
    • goroutine-协程-线程-进程
    • go值传递
    • go内存逃逸分析
    • go并发MGP模式
    • go并发控制
    • 垃圾回收-三色法
  • Istio
    • 服务网格
  • Jenkins
    • Jenkin On K 8 S
    • Jenkins Mac
  • Kubernetes
    • Deployment
    • k8s容器内查看-cpu-memory分配情况
    • kube-proxy原理
    • Kubernetes Informers
    • Kubernetes扩展点
    • Kubernetes部署策略
    • Pod Non Root
    • Pod驱逐
    • PV PVC Storage Class
    • Security Context
    • 优雅热更新
  • Python
    • Python-vs-Golang协程区别
  • Serviceless
  • Shell
    • Shell小技巧
  • VPN
    • OC Serv
  • Redis
Powered by GitBook
On this page
  • Golang json.Unmarshal
  • Map
  • slice 同理也需要

Was this helpful?

  1. Golang

Golang-json.Unmarshal

Golang json.Unmarshal

Map

json.Unmarshal 的时候,Map 必须要加上&

    m := make(map[string]json.RawMessage)
    err = json.Unmarshal([]byte(deployJson.Data.Marathon), &m)
package main

import (
    "fmt"
)

func mapFunc(m map[string]interface{}) {
    m = make(map[string]interface{})
    m["abc"] = "123"
}

func mapPtrFunc(mp *map[string]interface{}) {
    m := make(map[string]interface{})
    m["abc"] = "123"

    *mp = m
}

func main() {
    var m1, m2 map[string]interface{}
    mapFunc(m1)
    mapPtrFunc(&m2)

    fmt.Printf("%+v, %+v\n", m1, m2)
}
// output 
// map[], map[abc:123]
  • 首先,映射m1和m2都指向nil。

  • 调用mapFunc会将m1指向的值复制到m,结果m也将指向nil映射。

  • 如果在(1)中已经分配了 map ,则在(2)中,由m1指向的基础 map 数据结构的地址(不是m1的地址)将被复制到m。在这种情况下,m1和m都指向相同的 map 数据结构,因此m1也可以看到通过m修改 map 项。

  • 在mapFunc函数中,将分配新 map 并将其分配给m。无法将其分配给m1。

如果是指针:

  • 调用mapPtrFunc时,m2的地址将被复制到mp中。

  • 在mapPtrFunc中,将分配新 map 并将其分配给mp(不是mp)。由于mp是m2的指针,因此将新映射分配给mp将更改m2指向的值。请注意mp的值不变,即m2的地址。

slice 同理也需要

var slice []string
err := json.Unmarshal([]byte("[\"a\",\"b\",\"c\",\"d\"]"), &slice) //&slice
PreviousGolang InterfaceNextGolang Label

Last updated 3 years ago

Was this helpful?