🪟 Windows TippsUnable to install Docker Desktop on Windows 11(05.09.2026 um 05:19 Uhr)
🪟 Windows TippsIn dieser Woche kam das Aus für die ‘Deutsche Post’(05.09.2026 um 09:00 Uhr)
🔧 AI Nachrichten GenAI Workflows für Social Media Content(02.09.2026 um 14:00 Uhr)
🪟 Windows Tipps<b>Windows</b> - IT-Administrator.de(04.09.2026 um 04:17 Uhr)
🪟 Windows TippsUnable to install Docker Desktop on Windows 11(05.09.2026 um 05:19 Uhr)
🪟 Windows TippsIn dieser Woche kam das Aus für die ‘Deutsche Post’(05.09.2026 um 09:00 Uhr)
🔧 AI Nachrichten GenAI Workflows für Social Media Content(02.09.2026 um 14:00 Uhr)
🪟 Windows Tipps<b>Windows</b> - IT-Administrator.de(04.09.2026 um 04:17 Uhr)

26 🕛 kürzlich 3 Min Lesezeit
0

Go - Struct and Interface

↗ Quelle (dev.to)
🗣️ Stimme:

Hi Everyone,



Let's try to understand, Struct and Interface in Go programming language



Struct -



struct 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



but 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



what problem structs are solving ?



If 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.



for example -



suppose 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.



Now question is where we are storing this data ?



one 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



But 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




CODE
type User struct {
name string
email string
phone string
address string
}






so 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




CODE
var users [100]User

var users []User






Interview Answer -



Golang 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.



Interface -



An interface focuses on behavior instead of the actual type.



example 1:



suppose we have -



Dog

Cat

Human



All of them are different types, but all of them can perform one common action - Speak()



Now instead of writing separate code for Dog, Cat, and Human, we can write code that works with anything that can Speak().



This idea of focusing on behavior is called an interface.




CODE

package main

import "fmt"

type Speaker interface {
speak()
}

type Dog struct {}

func (d Dog) speak() {
fmt.Println("Dog")
}

type Cat struct {}

func (c Cat) speak() {
fmt.Println("Cat")
}

type Human struct {}

func (h Human) speak() {
fmt.Println("Human")
}

func speaking(s Speaker) {
s.speak()
}

func main() {
speaking(Dog{})
speaking(Cat{})
speaking(Human{})
}


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 50%
🟡 In Evaluierung 30%
🟢 Keine Auswirkung 15%
Spannende Innovation 5%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Unable to install Docker Desktop on Windows 11
1 Quelle
There is no volume selected in Diskpart error on Windows computer [Fix]
1 Quelle
'Call of Duty: Modern Warfare 2' PC remote code exploits have been fixed, says Activision
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Go - Struct and Interface

Thematisch verwandte Begriffe: Struct, Interface · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...