struct Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/struct/ Sat, 09 Oct 2021 03:18:08 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 struct Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/struct/ 32 32 159787465 Inheritance in GO using interface https://vikasboss.github.io/inheritance-go-interface/ https://vikasboss.github.io/inheritance-go-interface/#respond Fri, 09 Aug 2019 12:32:52 +0000 https://vikasboss.github.io/?p=145 This post describes inheritance using interface. Do visit our Inheritance in Go Complete Guide post for full reference Go supports inheritance by embedding struct or using interface. There are different ways of...

The post Inheritance in GO using interface appeared first on Welcome To Golang By Example.

]]>
This post describes inheritance using interface. Do visit our Inheritance in Go Complete Guide post for full reference

Go supports inheritance by embedding struct or using interface. There are different ways of doing it and each having some limitations. The different ways are:

  1. By using embedded struct – The parent struct is embedded in child struct. The limitation is that subtyping is not possible with this approach. You cannot pass the child struct to a function that expects base. Refer this link for more details –Inheritance using struct
  2. By using interfaces – Subtyping is possible but the limitation is that one has no way to refer to common properties. Current post describes this approach
  3. By using interface + struct – This fixes the limitations of above two approach but one limitation is that overriding methods is not possible. But there is a workaround. Refer to this link for more details – Inheritance using interface + struct

Details:

The child struct implements the methods of a common interface. This approach also solves the problem of subtyping. See below code

package main

import "fmt"

type iBase interface {
	say()
}

type child struct {
	style string
}

func (b *child) say() {
	fmt.Println(b.style)
}

func check(b iBase) {
	b.say()
}

func main() {
	child := &child{
		style: "somestyle",
	}
	child.say()
	check(child)
}

Output:

somestyle
somestyle

Limitation:

This approach has a limitation that it is not possible to refer to common properties as an interface can not have any properties. This problem is solved by the mixed approach of using Struct + Interface.

The post Inheritance in GO using interface appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/inheritance-go-interface/feed/ 0 145
Inheritance in GO using struct (Embedding) https://vikasboss.github.io/inheritance-go-struct/ https://vikasboss.github.io/inheritance-go-struct/#respond Fri, 09 Aug 2019 11:43:04 +0000 https://vikasboss.github.io/?p=139 This post desicribes inheritance using struct only. Do visit our Inheritance in Go Complete Guide post for full reference Go supports inheritance by embedding struct or using interface. There are different ways...

The post Inheritance in GO using struct (Embedding) appeared first on Welcome To Golang By Example.

]]>
This post desicribes inheritance using struct only. Do visit our Inheritance in Go Complete Guide post for full reference

Go supports inheritance by embedding struct or using interface. There are different ways of doing it and each having some limitations. The different ways are:

  1. By using embedded struct – The parent struct is embedded in child struct. The limitation is that subtyping is not possible with this approach. You cannot pass the child struct to a function that expects base. The current post describes this approach.
  2. By using interfaces – Subtyping is possible but the limitation is that one has no way to refer to common properties. Refer this link for more details – Inheritance using Interface
  3. By using interface + struct – This fixes the limitations of above two approach but one limitation is that overriding methods is not possible. But there is a workaround. Refer to this link for more details – Inheritance using interface + struct

Details:

In inheritance using a struct, a base struct is embedded in child struct and base properties and methods can directly be called on child struct. See below code:

package main

import "fmt"

type base struct {
	value string
}

func (b *base) say() {
	fmt.Println(b.value)
}

type child struct {
	base  //embedding
	style string
}

func check(b base) {
	b.say()
}

func main() {
	base := base{value: "somevalue"}
	child := &child{
		base:  base,
		style: "somestyle",
	}
	child.say()
	//check(child)
}

Output:

somevalue

Limitation:

Subtyping is not supported. You cannot pass the child struct to a function that expects base.

For example in the above code if you uncomment //check(child) it will give compilation error: “cannot use child (type *child) as type base in argument to check”. To fix this we can do inheritance using Interface

The post Inheritance in GO using struct (Embedding) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/inheritance-go-struct/feed/ 0 139
Interface to struct in Go (Golang) https://vikasboss.github.io/interface-to-struct/ https://vikasboss.github.io/interface-to-struct/#respond Mon, 18 Mar 2019 16:38:18 +0000 https://vikasboss.github.io/?p=73 We come around a situation sometimes in programming where an empty interface might a struct internally and we have to get the concrete struct out of it. Whoever is not aware of...

The post Interface to struct in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
We come around a situation sometimes in programming where an empty interface might a struct internally and we have to get the concrete struct out of it. Whoever is not aware of what an empty interface is should read this excellent article: https://research.swtch.com/interfaces.

For conversion of interface{} to a struct, we will use the library – https://github.com/mitchellh/mapstructure . Let’s understand how to convert the interface to a struct by an example:

package main

import (
	"fmt"

	"github.com/mitchellh/mapstructure"
)

type NewCustomerEvent struct {
	Name  string
	Phone string
	Email string
}

func main() {
	newCustomer := NewCustomerEvent{Name: "x", Phone: "082213909101", Email: "xyz@gmail.com"}
	convert(newCustomer)
}

func convert(event interface{}) {
	c := NewCustomerEvent{}
	mapstructure.Decode(event, &c)
	fmt.Printf("Event is: %v", c)
}

Output:

Event is: {x 082213909101 xyz@gmail.com}

The post Interface to struct in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/interface-to-struct/feed/ 0 73