mirror of
https://github.com/foomo/foomo-docs.git
synced 2025-10-16 12:35:40 +00:00
1 line
5.9 KiB
JavaScript
1 line
5.9 KiB
JavaScript
"use strict";(self.webpackChunkfoomo=self.webpackChunkfoomo||[]).push([[3181],{1259:(e,t,a)=>{a.d(t,{X:()=>p});var o=a(7462),n=a(2801),l=a(7294),i=a(4991),s=a(9030),r=a(3746),d=a(9960);const p=e=>{const[t,a]=(0,l.useState)(!1),[p,c]=(0,l.useState)(""),u=(0,n.r)();(0,l.useEffect)((()=>{""==p&&""!==e.id&&fetch("https://docs.bestbytes.com/services/playground?id="+e.id,{mode:"cors"}).then((e=>e.text().then((e=>c(e))))).catch((e=>c("can not load source yet")))}),[p]);const m="https://goplay.tools/snippet/"+e.id;return l.createElement(l.Fragment,null,l.createElement("div",{style:{paddingBottom:"1rem",width:"100%"}},t?l.createElement(l.Fragment,null,l.createElement("button",{className:"button button--primary",onClick:e=>{a(!1)}},"close playground")," ",l.createElement("button",{className:"button button--primary"+(t?"":" disabled"),onClick:e=>{u.enter()}},"fullscreen")," "):l.createElement("button",{className:"button button--primary",onClick:e=>{a(!0)}},"open playground")," ",l.createElement(d.Z,{href:m},"go to ",m)),!t&&l.createElement(r.ZP,(0,o.Z)({},r.lG,{code:p,language:"go"}),(e=>{let{className:t,style:a,tokens:o,getLineProps:n,getTokenProps:i}=e;return l.createElement("pre",{className:t,style:a},""==p?"... loading sources":"",o.map(((e,t)=>l.createElement("div",n({line:e,key:t}),e.map(((e,t)=>l.createElement("span",i({token:e,key:t}))))))))})),l.createElement("div",{style:{display:t?"block":"none"}},l.createElement(s.k,{topic:"load external go playground, with all it\xb4s potentially evil cookies coming from https://goplay.tools",id:"goPlaygroundIsCool"},l.createElement("div",null,l.createElement(n.I,{handle:u},l.createElement(i.h,{src:m,proportion:e.proportion}))))))}},4991:(e,t,a)=>{a.d(t,{h:()=>n});var o=a(7294);const n=e=>{let{proportion:t,src:a,style:n}=e;return t||(t=4/3),n&&(n={}),o.createElement("div",{style:{width:"100%",height:0,paddingTop:100/t+"%",position:"relative",float:"left",...n}},o.createElement("iframe",{style:{width:"100%",height:"100%",display:"block",position:"absolute",top:0,left:0},src:a,frameBorder:"0",scrolling:"no",allowFullScreen:!0}))}},9030:(e,t,a)=>{a.d(t,{k:()=>l});var o=a(7294);const n="undefined"==typeof localStorage,l=e=>{const[t,a]=(0,o.useState)((l=e.id,"undefined"!=typeof localStorage&&localStorage.getItem(l)));var l;return(0,o.useEffect)((()=>{console.log("well it is cool",e.id,{isCool:t,SSR:n})}),[t,n]),t?e.children:o.createElement("div",null,o.createElement("button",{className:"button button--lg button--secondary",onClick:t=>{localStorage.setItem(e.id,"yes"),a(!0)}},e.topic))}},9220:(e,t,a)=>{a.r(t),a.d(t,{assets:()=>d,contentTitle:()=>s,default:()=>m,frontMatter:()=>i,metadata:()=>r,toc:()=>p});var o=a(7462),n=(a(7294),a(3905)),l=a(1259);const i={title:"Maps",sidebar_position:3,tags:["Go Basics"]},s="Maps",r={unversionedId:"backend/go-by-example/nil-maps",id:"backend/go-by-example/nil-maps",title:"Maps",description:"Maps are unordered key value pairs where each key is unique.",source:"@site/docs/backend/go-by-example/nil-maps.mdx",sourceDirName:"backend/go-by-example",slug:"/backend/go-by-example/nil-maps",permalink:"/docs/backend/go-by-example/nil-maps",draft:!1,editUrl:"https://github.com/foomo/foomo-docs/tree/main/foomo/docs/backend/go-by-example/nil-maps.mdx",tags:[{label:"Go Basics",permalink:"/docs/tags/go-basics"}],version:"current",sidebarPosition:3,frontMatter:{title:"Maps",sidebar_position:3,tags:["Go Basics"]},sidebar:"backendSidebar",previous:{title:"Panic and Recover",permalink:"/docs/backend/go-by-example/panic-and-recover"},next:{title:"Ranging and Looping",permalink:"/docs/backend/go-by-example/ranging"}},d={},p=[{value:"What you should now about nil maps",id:"what-you-should-now-about-nil-maps",level:2}],c={toc:p},u="wrapper";function m(e){let{components:t,...a}=e;return(0,n.kt)(u,(0,o.Z)({},c,a,{components:t,mdxType:"MDXLayout"}),(0,n.kt)("h1",{id:"maps"},"Maps"),(0,n.kt)("p",null,"Maps are unordered key value pairs where each key is unique.\nUnordered in that context means that the order can be different every time you iterate over its contents.\nYou can add, update, get, or delete keys and values."),(0,n.kt)("p",null,"Map lookup operations will always return the value for the requested key and a boolean indicated whether the key was found.\nIn case the key was not found, the value will the default value for its data type (eg false for booleans, empty string for strings, nil for pointers etc)"),(0,n.kt)("pre",null,(0,n.kt)("code",{parentName:"pre",className:"language-go"},' musicMap := make(map[string]string)\n\n musicMap["Jimi"] = "Hendrix" // Add key and value pair\n musicMap["Jimi"] = "Hendrix \u2764\ufe0f" // Update value\n\n v, found := musicMap["Jimi"] // Get a value for an existing key, will return found==true\n v, found := musicMap["Jimmy"] // Get value for a non existing key: will return found==false\n\n delete(musicMap, "Jimi") // Deletes Jimi key value pair\n')),(0,n.kt)("h2",{id:"what-you-should-now-about-nil-maps"},"What you should now about nil maps"),(0,n.kt)("p",null,"Consider the following two maps:"),(0,n.kt)("pre",null,(0,n.kt)("code",{parentName:"pre",className:"language-go"}," var map1 map[string]int\n map2 := make(map[string]int)\n")),(0,n.kt)("p",null,"What is the difference between these two? If you will add keys to one of them the program will panic while you can add keys\nwithout a problem to the other. Map1 is a ",(0,n.kt)("em",{parentName:"p"},"nil map"),", because it has not been initialized. Map2 is an empty map but because it is initialized\nwith the make statement you can add elements to this map later on in your program. Nil maps could for example be used for structs that have optional data. In that case\nwe don't want to allocate them every time, only when the data is present. "),(0,n.kt)("br",null),(0,n.kt)("p",null,"While you can not add elements to a nil map, read operations work just fine. Try it out yourself in the following example:"),(0,n.kt)(l.X,{id:"Alpsx5uHXCS",proportion:1.6,mdxType:"GoPlayground"}))}m.isMDXComponent=!0}}]); |