Skip to main content

Structs in Golang

A struct in Golang is a user-defined type that allows to combine different types of variables into single type.
In Golang, Structures does not support inheritance but supports composition.
We can define a struct using a keyword struct.
Syntax: type struct_name struct{}

Example:

 package main
 import "fmt"
 type User struct {
	name string
	age  int
 }
 func main() {
	// declare a struct variable
	var user1 User
	user1.name = "vicky"
	user1.age = 22
    
	// declaration and initialization of struct at a same to a struct variable
	user2 := User{"tejas", 23}
    
	// you can define a struct address to a varibale while initializing
	user3 := &User{"vinu", 23}
	fmt.Println("user 1 : "user1)
	fmt.Println("user 2 : "user2)
	fmt.Println("user 3 : "*user3)
 }

Output:

user 1 :  {vicky 22}
user 2 :  {tejas 23}
user 3 :  {vinu 23}

Anonymous Struct in Golang:

A anonymous struct is a struct with no name. It's useful when you want create a one time useable struct.

Example for Anonymous struct:

 package main
 import "fmt"
 func main() {

	// struct with no struct name
	user1 := struct {
		name string
		age  int
	}{"vicky", 22}

	// struct with no struct name and no field names
	user2 := struct {
		string
		int
	}{"tejas", 23}

	fmt.Println("user 1 : ", user1)
	fmt.Println("user 2 : ", user2)
 }

Output:

user 1 :  {vicky 22}
user 2 :  {tejas 23}

Important Note: if we use an Anonymous struct with no field names, same types of variables is not allowed

 package main
 import "fmt"
 func main() {
	// struct with no struct name and no field names
	s1 := struct {
		int
		int
	}{23, 23}
	fmt.Println(s1)
 }

Output:

./prog.go:9:3: int redeclared
	./prog.go:8:3: other declaration of int
./prog.go:10:8: too many values in struct literal of type struct{int}

Comments

Popular posts from this blog

Why Go Language?

Go Language: Open Source Programming language Statically Typed language Makes sharing code easy Similar to C programming language Organization that use Go includes Google, Docker, Kubernetes, Cloudflare, Dropbox, Netflix & Uber. Why Go language? C like Syntax Compiles to native code i.e. one executable file is need to run the whole program. Garbage collection Concurrency built-in language Compatibility promise → Once a program written to the go one specification will continue to compile and run correctly, over the lifetime of that specification. Why Go Compiler is fast? Simple and Minimalistic (20 keywords only) Does not allows unused dependencies No circular dependencies Does not use header files Solving Modern problems with Go: Go has concise syntax with few keywords to remember. Languages like C or C++ offers fast execution, whereas languages like Ruby or Python offers rapid application development, Go bridges these computing worlds and offers development fast. Modern Computers ...

Copy function in Golang

How Copy function works ? The built-in copy function copies the source element into the destination element and returns the destination element length.   func Copy( dst , src [] Type) int There are some special case using this function which is we can copy a string into an byte slice variable   func Copy( dst []byte, src string) int Examples: package main import "fmt" func main() { var arr = make([]string, 2) n := copy(arr, []string{"hi", "hello", "welcome", "ll"}) fmt.Println("element length :", n) fmt.Println("values :", arr) } Output: element length : 2 values : [hi hello] Special Case example: package main import "fmt" func main() { var b = make([]byte, 5) // copy from a string to byte slice n := copy(b, "vicky") fmt.Println("element length :", n) fmt.Println("values :", string(b)) } Output: element length : 5 values : vicky

Comparing Structs in Go

We can compare two structs in two ways:     1. Equal to (==) Operator     2. DeepEqual() Method in reflect package Example using (==) Operator : package main import "fmt" type User struct { name string age int } func main() { u1 := User{"tejas", 23} u2 := User{"tejas", 23} u3 := User{"vicky", 22} fmt.Println("Comparing u1 and u2 struct :", u1 == u2) fmt.Println("Comparing u1 and u3 struct :", u1 == u3) } Output: Comparing u1 and u2 struct : true Comparing u1 and u3 struct : false Example using DeepEqual() Method : package main import ( "fmt" "reflect" ) type User struct { name string age int } func main() { u1 := User{"tejas", 23} u2 := User{"tejas", 23} u3 := User{"vicky", 22} fmt.Println("Comparing u1 and u2 struct :", reflect.DeepEqual(u1, u2)) fmt.Println("Comparing u1 and u3 struct :", reflect.DeepEqual(u1, u3)) } Outpu...