golang interface

package main

import (
	"fmt"
)

type Pricing interface {
	getDiscount() float32
}

type Address interface {
	getAddress() string
}

// nested struct
type ShowRoom struct {
	address    string
	location   string
	postalcode int
}

// sub nested struct
type Car struct {
	id       int
	name     string
	price    float32
	showroom ShowRoom
}

type Book struct {
	Name, Author string
}

func (m Car) getDiscount() float32 {
	return m.price * 0.5
}

func (m Car) getAddress() string {
	return m.showroom.address
}

func getAllDiscount(d Pricing) float32 {
	return d.getDiscount()
}

func getAllAddress(a Address) string {
	return a.getAddress()
}

func main() {

	var SuperCar Car

	var CityName interface{}
	CityName = "jakarta"
	v, ok := CityName.(string)

	var users = []map[string]interface{}{
		{"name": "monkey d lufy", "age": 19},
		{"name": "trafagar d law", "age": 23},
		{"name": "nico robin", "age": 20},
	}

	SuperCar.id = 1
	SuperCar.name = "lambogini"
	SuperCar.price = 1.500000000
	SuperCar.showroom.address = "jl.nusantara kap 50 - 51"
	SuperCar.showroom.location = "depok"
	SuperCar.showroom.postalcode = 163353

	SuperCar.id = 2
	SuperCar.name = "mc.claren"
	SuperCar.price = 2.500000000
	SuperCar.showroom.address = "jl.kalimulya kap 200 - 2001"
	SuperCar.showroom.location = "depok"
	SuperCar.showroom.postalcode = 163334

	if !ok {
		fmt.Printf("failed to read assertion type with interface %#v \n", v)
		fmt.Printf("failed to read assertion type with interface status %#v \n", ok)
	} else {
		fmt.Printf("successfully read assertion type with interface %#v \n", v)
		fmt.Printf("successfully read assertion type with interface status %#v \n", ok)
	}

	fmt.Println("read nested struct", SuperCar)
	fmt.Println("read nested struct with key", SuperCar.showroom.location)
	fmt.Println("read nested struct discount with interface method", getAllDiscount(SuperCar))
	fmt.Println("read nested struct address with interface method", getAllAddress(SuperCar))
	fmt.Println("read all users with interface assertion", users)
}

3.88
8
Tori 100 points

                                    type Shape interface {
    area() float64
    perimeter() float64
}

3.88 (8 Votes)
0
4.3
10
Skylark 65 points

                                    The interface is a collection of methods as well as it is a custom type.

4.3 (10 Votes)
0
Are there any code examples left?
New code examples in category Go
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to implement any interface in golang make interface golang declare custom interface golang how to use interfaces in go what are interface in go go []interface{} interface en golang implement golang interface interface to uint golang interface in golang api golang new interface what is an interface on go how to declare interface in golang how to create interface in go when use interface in golang golang can interface have variables examples on interface in golang golang initialize interface golang interface name interface on golang defining interfaces in go lang golang interface method golang how to use interface in function using interface as object golang interface{} example in golang interface{} example in go lang how to initialize interface in golang create an interface in golang how to accesss an interface golang declare interface golang all implemented interfaces in go go interface syntax what is a go interface set interface method in golang interface{} in go how to use interface go Golang interface function declare interface variable golang go how to use interface what is an interface in go assigning function to interface golang call interfaces in golang explained declare interface go go interfaces explained golang interface functions golang interface programs interface method golang golang implement interface golang create an interface interface go does go have interfaces que son interfaces golang how to use Go Interfaces how to implement interface in golang access interface inside interface golang create interface golang go interfaces Interface extends golang how to implement golang interface create interface in golang golang declare interface methods interface in golang simple way what is an interface in golang golang interface package golang interface uses interfaces interface type in golang golang what's the use of interface golang interface sample what is a golang interface interface function used as a value golang golang variables interface define interface golang golang code interface define interface in golang function to instantiate an interface golang golang initialise interface go ...interface{} golang struct interface class interface golang interface in go' go interface implementation implement an interface in golang golang interface defination what is an interface golang interface type golang interface with attributes golang interface with go interface in go how work the interface in go interface on go language defining interface golang how efficient are golang interface implement interface in golang var interface golang go interface{} interface inside interface golang .([]interface{}) in golang what is interface in golang interfaces in go go implement interface golang golang interface what is ...interface golang ...interface golang how to type an interface golang golang function interface interfaces go declare interface with value golang interface to int golang golang interface example interface implementation golang go define interface learn interface golang Interface() golang interface tutorial interface go by example []interface{} go what are interfaces in go interface methods golnag go interfacea use of interface in golang create instance of interface golang create instance of interface golan interface golang example golang instance of interface interface in golang interface.() using interfaces golang go by example interface interfaces.go how to implement an interface in golang what is ...interface in golang go interface examples golang tutorial methods and interface golang tutorial methods interface Go Type interface interface use in golang inrefaces with go golang implement an interface type interface golang go interface go lang interface golang model interface implement interface golang interfaces in golang interface golang golang interface
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source