Maps, Structs and More.

Maps, Structs and More.

While arrays and slices form the core of sequential data structures, maps, interfaces, and structs offer unique capabilities, especially when transitioning from a language like TypeScript. Lets talk about them today.

Maps: Go's Key-Value Store:

A map in Go is a composite type that represents a hash table or a dictionary or object/map in typescript's case. It associates keys and values where each key maps to a unique value. The key can be of any type for which the equality operation is defined, such as integers, floats, strings, etc.

m := make(map[string]int) 
m["apple"] = 1 
m["banana"] = 2        


Maps and JSON : Marshaling and Unmarshaling of Data

Given the nature of maps, they easily translate to JSON objects. The Go standard library provides easy-to-use encoding and decoding capabilities for this.

import "encoding/json"

person := map[string]string{

"name": "Shashank",

"city": "Delhi,

}
jsonData, err := json.Marshal(person)

if err != nil {

panic(err)

}
fmt.Println(string(jsonData)) // Outputs: {"city":"Delhi,"name":"Shashank"}        

Structs: Grouping Data Together

A struct in Go is another composite type that groups together zero or more values with named fields. They're useful for defining and grouping data.

type Person struct {

    Name string

    Age  int

}        

Structs and JSON

Marshaling a struct to JSON is straightforward, with field names being used as default keys

p := Person{Name: "may", Age: 30}

jsonData, err := json.Marshal(p)

if err != nil {

    panic(err)

}

fmt.Println(string(jsonData)) // Outputs: {"Name":"may","Age":30}        

Structs as "Classes"

Itis well known design choice of go to not support class based OOP but If you're coming from a TypeScript /C#/Java background, you may miss the class-based approach. In Go, structs combined with methods can be used to simulate classes.

func (p *Person) SayHello() {

    fmt.Println("Hello, my name is", p.Name)

}

may := Person{Name: "may"}

may.SayHello()         

Conclusion

The Go programming language, with its robust yet straightforward approach to data structures and types, offers developers the tools to build efficient, maintainable applications. Whether it's the key-value pairing of maps, the behavioral contracts of interfaces, or the data grouping capabilities of structs, understanding these concepts is crucial when working with Go, especially if transitioning from TypeScript or another object-oriented language.

To view or add a comment, sign in

More articles by Shashank Shekhar

  • Learnings from Agentic Automation

    Over the past few months, I’ve been learning and building AI Agents / LLM-driven automation workflows. I learned a lot…

    1 Comment
  • Why “Vibe Coding” Doesn’t Work for Me

    For the record I am talking about vibe coding — a term popularized by Andrej Karpathy, where you let AI agents write…

  • Chat server with Websocket in Node JS

    WebSockets are powerful tools for real-time communication, allowing clients and servers to maintain an open connection…

    1 Comment
  • Async IO programming with Node js

    Introduction Asynchronous programming is a technique that enables your program to start a potentially long-running task…

  • Typescript Programming Guide

    Introduction By now, TypeScript has emerged as a fully-fledged de facto standard for writing secure, enterprise-grade…

  • Error as values and Return Types in Go

    Error Handling in Go is bit different than traditional way you must have seen in programming in languages like…

    2 Comments
  • Arrays vs Slices

    With this article I have tried giving a brief overview of two fundamental data structures in the Go programming…

  • Pointers in Go

    In Go, a pointer is a variable that stores the memory address of another variable. We use the ampersand (&) operator to…

  • Understanding the Distinctions: never vs void

    In TypeScript, the never and void types serve distinct purposes, despite their similarities in indicating absence of…

    1 Comment
  • Developing Good Coding Habits with AI-Powered Code Reviews

    As developers, we are always looking to improve our coding skills and write better code. One way to achieve this is by…

Explore content categories