{"slug": "go-struct-and-interface", "title": "Go - Struct and Interface", "summary": "In Go, a **struct** is a custom data type that groups related fields together, allowing programmers to model real-world entities (like a User with name, email, and phone) when built-in types are insufficient. An **interface**, on the other hand, defines behavior by specifying a set of methods; any type that implements those methods satisfies the interface, enabling polymorphic code that works across different types (e.g., Dog, Cat, and Human all satisfying a `Speaker` interface with a `speak()` method). Together, structs and interfaces provide a flexible way to structure data and define shared behaviors in Go programs.", "body_md": "Hi Everyone,\n\nLet's try to understand, Struct and Interface in Go programming language\n\n**Struct** -\n\nstruct is also called as structure, so normally we have some built in data types which are provided by the language, such as int, float64, string, bool etc\n\nbut suppose we want to create our own custom data type, how can we achieve this ? so in order to solve this problem we can use struct which helps us to create our own custom data type in which related data is grouped together, once we have this new custom data type, we can use it in our programs, like when when declaring a variable or when declaring and assigning the value\n\n**what problem structs are solving ?**\n\nIf we want to represent a real-world entity in our program we don't have any type for it, so to solve this problem, Golang provides us structs, which allow us to group related data together and create a custom type.\n\n**for example -**\n\nsuppose we want to store data of 100 users, so we have a program which ask us to enter details of a user like name, email, phone and address and then storing it, and after that it will ask us whether we want add the data of another user or not, this we way we need to add the data of 100 users.\n\nNow question is where we are storing this data ?\n\none approach is we create separate arrays or slices for each property and then store the data , but the problem is, the data is not grouped together as it is stored separately\n\nBut what we wanted to achieve, is to keep the related data together right, hence we use use structs which helps us to create a type of similar data\n\n```\ntype User struct {\n    name    string \n    email   string\n    phone   string\n    address string\n}\n```\n\nso to solve above problem of storing data of 100 users, what we can do is since we have a new type \"User\", so we can create an array or a slice of \"User\" type\n\n``` js\nvar users [100]User\n\nvar users []User\n```\n\n**Interview Answer** -\n\nGolang provides us built-in data types such as int, float64, string, bool etc. But when we want to represent a real-world entity in our program we don't have any type for it, so to solve this problem, Golang provides us structs, which allow us to group related data together and create a custom type.\n\n**Interface** -\n\nAn interface focuses on behavior instead of the actual type.\n\n**example 1:**\n\nsuppose we have -\n\nDog\n\nCat\n\nHuman\n\nAll of them are different types, but all of them can perform one common action - Speak()\n\nNow instead of writing separate code for Dog, Cat, and Human, we can write code that works with anything that can Speak().\n\nThis idea of focusing on behavior is called an interface.\n\n```\npackage main \n\nimport \"fmt\"\n\ntype Speaker interface {\n    speak()\n}\n\ntype Dog struct {}\n\nfunc (d Dog) speak() {\n    fmt.Println(\"Dog\")\n}\n\ntype Cat struct {}\n\nfunc (c Cat) speak() {\n    fmt.Println(\"Cat\")\n}\n\ntype Human struct {}\n\nfunc (h Human) speak() {\n    fmt.Println(\"Human\")\n}\n\nfunc speaking(s Speaker) {\n    s.speak()\n}\n\nfunc main() {\n    speaking(Dog{})\n    speaking(Cat{})\n    speaking(Human{})\n}\n```\n\n", "url": "https://wpnews.pro/news/go-struct-and-interface", "canonical_source": "https://dev.to/ayush_gupta_70e4746734c34/go-struct-and-interface-3goe", "published_at": "2026-05-23 11:59:31+00:00", "updated_at": "2026-05-23 12:32:16.100400+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Golang"], "alternates": {"html": "https://wpnews.pro/news/go-struct-and-interface", "markdown": "https://wpnews.pro/news/go-struct-and-interface.md", "text": "https://wpnews.pro/news/go-struct-and-interface.txt", "jsonld": "https://wpnews.pro/news/go-struct-and-interface.jsonld"}}