Reflections in Go

func (d *decodeState) unmarshal(v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &InvalidUnmarshalError{reflect.TypeOf(v)}
}
d.scan.reset()
d.scanWhile(scanSkipSpace)
// We decode rv not rv.Elem because the Unmarshaler interface
// test must be applied at the top level of the value.
err := d.value(rv)
if err != nil {
return d.addErrorContext(err)
}
return d.savedError
}
fmt.Println(reflect.TypeOf(2)) // 2
interface value => dynamic operand's type + dynamic operand's value
x := 2
fmt.Printf("%T\n", x) // int
x := 2
fmt.Println(reflect.ValueOf(x)) // 2
x := reflect.ValueOf(2) // a reflect.Value
fmt.Println(x) // 2
t := x.Type()
fmt.Println(t) // int
i := x.Interface() // an interface{}
v := x.(int)
fmt.Println(v) // 2
x := reflect.ValueOf(2)
fmt.Println(x.Kind()) // int
package main

import (
"fmt"
"reflect"
)

type Transactions struct {
TransactionID string `json:"txn_id,omitempty"`
Amount float64 `json:"amount,omitempty"`
}

func printTypes(i interface{}) {
type := reflect.TypeOf(i)
kind := t.Kind()
fmt.Println("Type ", type)
fmt.Println("Kind ", kind)
}
func main() {
structForTxn := Transactions{
TransactionID: "abc-1231-cd",
Amount: 100,
}
printTypes(structForTxn)
}
Type  main.Transactions
Kind struct

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Case Study On Expert System

READ/DOWNLOAD@< Autodesk Revit 2017 for Architectu

Setting up a Fluid Structure Interaction Workflow with OpenFOAM and deal.II

Creating a database using MSSQL SSIS Toolbox and Using topojson files to visualise data based on…

Google Africa Scholarship, a success story.

Debugging EVM bytecode with radare2

Getting started with Django using VS Code on Windows 10

Appsecco hosting a private AllDayDevOps 2020 Virtual Viewing Party

Swag sent to us by the organisers of AllDayDevOps 2020

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aman Jain

Aman Jain

Software Engineer

More from Medium

Part-of-speech tag a string, filter to pronouns in Go

How I got “Hot reloading” to work with Golang and nodemon (UNIX)

Welcome to gRPC, Please Follow Me…

Efficient ways to call .env