#!/usr/bin/env python3 """ Linear regression learning algorithm """ class LinearRegression: def train(self, x, y): """ Trains a linear model from the data set Args: x -> independent variable matrix y -> dependent variable column vector Return: List of learn co-efficients """ #Augmenting idv with constant intercept value for xi in x: xi.insert(0, 1) no_idv = len(x[0]) #Todo: make it a hyper param alpha = 0.001 #Todo: randomization theta = [0] * no_idv theta_temp = [t for t in theta] #Convergence criteria is -> iterating 100000 times, this is for the sake of impl for j in range(0, 100000): for i in range(0, no_idv): theta[i] = theta_temp[i] - alpha * self._partial_diff(theta_temp, x, y, i) theta_temp = [t for t in theta] return theta def _partial_diff(self, theta, x, y, index): """ Computes the paritial differentiation of htheta Args: theta -> coefficients row vector x -> independent variable matrix y -> dependent variable column vector index -> coefficient term being differentiated Return: updated coefficient at given 'index' """ no_idv = len(x[0]) m = len(x) sigma = 0.0 for i in range(0, m): htheta = self._htheta(theta, x, i) sigma = sigma + (htheta - y[i][0]) * x[i][index] return sigma / m def _htheta(self, theta, x, index): """ htheta function computation Args: theta -> coefficient row vector x -> independent variable matrix index -> row index of x """ no_idv = len(x[0]) htheta = 0.0 for i in range(0, no_idv): htheta = htheta + theta[i] * x[index][i] return htheta def main(): regression = LinearRegression() #y = 2 + x x = [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]] y = [[3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13]] thetas = regression.train(x, y) print(thetas) for xi in x: htheta = thetas[0] * 1 + thetas[1] * xi[1] print("{xi} -> {htheta}".format(xi=xi[1], htheta=htheta)) if __name__ == "__main__": main()

# A quickly prototyped file generator

This file generator generates new files every 2 seconds in the specified folder.

import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.UUID; import java.util.stream.Stream; public class FileGen { public static void main(String[] args) throws InterruptedException { if (0 == args.length) { System.out.println("Usage: java -jar FileGen.jar /tmp/{folder}"); System.exit(1); } final String outputFolder = args[0]; while (true) { String fileName = outputFolder + "/" + UUID.randomUUID().toString(); File file = new File(fileName); try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); String timeStamp = LocalDateTime.now().toString(); String category = getRandomCategory(); String type = getRandomType(); int score = getRandomScore(); String newLine = timeStamp + "," + category + "," + type + "," + score; bw.write(newLine); bw.close(); } catch (IOException e) { System.out.println("Exiting.. : Unable to create file " + fileName); break; } Thread.sleep(2000); } Runtime.getRuntime().addShutdownHook(new Thread(() -> { System.out.println("Cleaning up " + outputFolder); File folder = new File(outputFolder); Stream.of(folder.listFiles()) .forEach(f -> f.delete()); })); } private static List<String> categories = Arrays.asList("A", "B", "C"); private static List<String> types = Arrays.asList("2", "2", "3"); private static String getRandomCategory() { Random random = new Random(); return categories.get(random.nextInt(categories.size())); } private static String getRandomType() { Random random = new Random(); return types.get(random.nextInt(types.size())); } private static int getRandomScore() { return new Random().nextInt(21); } }

# Lucene – Indexing and Searching: A copy paste programmer’s initial source

These dependencies should be present in the classpath

<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>7.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>7.3.1</version> </dependency>

Source code

package org.kseg; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.*; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import java.io.File; import java.io.IOException; public class LuceneIndexer { public static void main(String[] args) throws IOException, ParseException { String indexFolder = args[0]; System.out.println(indexFolder); //Creating instance of directory, this is where the indices will be stored Directory indexDirectory = FSDirectory.open(new File(indexFolder).toPath()); //Stop word removal and other config related to indexing IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer()); //If index is not present it will be created or else will be appended, that's what this config instructs indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(indexDirectory, indexWriterConfig); indexDoc(writer, "alcohol"); indexDoc(writer, "dimethyl alcohol"); indexDoc(writer, "dimethyl"); indexDoc(writer, "2,3 Polyacid(34 Daa)"); indexDoc(writer, "5,6 Polyacid(3,(4 Daa))"); writer.close(); //Query parser constructs the query object of searching, there are different types of queries, need to explore //more Query query = new QueryParser("chemical", new StandardAnalyzer()).parse("the product name is Polyacid"); //Opens index for reading IndexReader reader = DirectoryReader.open(indexDirectory); //Hope rest of the code is self explanatory IndexSearcher searcher = new IndexSearcher(reader); TopDocs docs = searcher.search(query, 3); ScoreDoc[] scoreDocs = docs.scoreDocs; System.out.println("Max Hits " + docs.totalHits + ", Max Score " + docs.getMaxScore()); for (ScoreDoc scoreDoc: scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); System.out.println(doc.get("chemical") + ", score " + scoreDoc.score); } } //Creates and index a document private static void indexDoc(IndexWriter writer, String chemical) throws IOException { Document document = new Document(); document.add(new TextField("chemical", chemical, Field.Store.YES)); writer.addDocument(document); } }

# Set as a function abstraction – Part 2

In Part 1 we saw how to define basic operation like union, intersection etc on Set in terms of its indicator function.

In this section we will see Universal quantification, Existential quantification , map and reduce operations.

**Universal Quantification**

p holds for all the values in Set s. Here there is a notion of value (till this function we were operating in an abstract space). The function abstraction Int => Boolean indicates whether an element is present in a set or not, so inorder to find the values of Set s we will have to iterate from -infinity to +infinity and apply p to each value, which is not at all possible and hence we are limiting our bound to a finite range from -1000 to +1000

/** * The bounds for `forall` and `exists` are +/- 1000. */ val bound = 1000 /** * Returns whether all bounded integers within `s` satisfy `p`. */ def forall(s: Set, p: Int => Boolean): Boolean = { def iter(e: Int): Boolean = { if (e > bound) true else if (contains(s,e) && !p(e)) false else iter(e + 1) } iter(-bound) }

p holds for all values in s if !p holds for at-least one value in s,

proof by contradiction

**Existential Quantification**

at-least one value in s satisfies p

/** * Returns whether there exists a bounded integer within `s` * that satisfies `p`. */ def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, e => !p(e))

there exists a bounded integer within s that satisfies p if !p doesn’t satisfy for all values in s,

proof by contradiction.

**map**

/** * Returns a set transformed by applying `f` to each element of `s`. */ def map(s: Set, f: Int => Int): Set = { def iter(s: Set, acc: Set, e: Int): Set = { if(e > bound) acc else if(contains(s, e)) iter(s, union(acc, x => f(e) == x), e + 1) else iter(s, acc, e + 1) } iter(s, x => false, -bound) }

let s1 = [1,2,3] we want to square it and get s2 = [1, 4, 9].

indicator function for s1 = x => x == 1 || x == 2 || x == 3 and

for s2 = x => x == 1 || x == 4 || x == 9

so the function f = x => x * x, applying f on all values in s1 results in s2

there for s2 = x => x == f(1) || x == f(2) || x == f(3)

**reduce**

In addition to these functions, I have added the reduce function on set

def reduce(s: Set, f: (Int, Int) => Int ): Option[Int] = { @tailrec def loop(s: Set, acc: Int, e: Int): Int = { if(e > bound) { acc } else if(s(e)) { loop(s, f(acc, e), e + 1) } else { loop(s, acc, e + 1) } } @tailrec def getFirst(s: Set, e: Int): Option[Int] = { if(e > bound) { None } else if(s(e)) { Some(e) } else { getFirst(s, e + 1) } } val firstElem = getFirst(s, -bound) if(firstElem.isEmpty) { None } else { Some(loop(s, firstElem.get, firstElem.get + 1)) } }

# Set as a function abstraction – Part 1

I have been attending sessions on Scala at Hifx (taken by Sajith Bhai as part of our internal training programs). If I get it right the sessions started with Scala syntax and eventually moved towards functional thinking and functional apis on Scala collections, and as a exercise we were given an assignment to implement union, intersection, diff, filter, forall, exists, map on Set.

This was too abstract a task.

At first sight we would assume that this is a data structure problem. This will we torn away by the alias definition

type Set = Int => Boolean

. This is the signature of the characteristics or indicator function of the Set (Indicator_function_wiki) and this is all we will get for writing our implementation.

**contains**

def contains(s: Set, elem: Int): Boolean = s(elem)

this impl is given with the assignment. assume that s: Set = x => x > 4, then contains is s(elem) = elem => elem > 4.

So as the name suggests if we give an Set and an element to contains it will tell us whether the element is present in the Set.

**Singleton Set**

def singletonSet(elem: Int): Set = x => elem == x

Singleton set reuturns a Set abstraction which will be evaluated to true for a single element in the domain of the function.

singletonSet(1) == x => 1 == x, which will be true only for 1

**union**

/** * Returns the union of the two given sets, * the sets of all elements that are in either `s` or `t`. */ def union(s: Set, t: Set): Set = (x: Int) => contains(s, x) || contains(t, x)

the sets of all elements that are in either `s` or `t`.

let s = x => x % 3 == 0

let t = x => x % 5 == 0

then union = x => x % 3 == 0 || x => x % 5 == 0

(This is equivalent to the logical connective OR in predicate logic)

**intersection**

/** * Returns the intersection of the two given sets, * the set of all elements that are both in `s` and `t`. */ def intersect(s: Set, t: Set): Set = (x: Int) => contains(s, x) && contains(t, x)

(This is equivalent to the logical connective AND in predicate logic)

let s = x => x % 3 == 0

let t = x => x % 5 == 0

then intersection = x => x % 3 == 0 && x => x % 5 == 0

**diff**

/** * Returns the difference of the two given sets, * the set of all elements of `s` that are not in `t`. */ def diff(s: Set, t: Set): Set = (x: Int) => contains(s, x) && !contains(t, x)

Self explanatory I guess 😉

**filter**

/** * Returns the subset of `s` for which `p` holds. */ def filter(s: Set, p: Int => Boolean): Set = intersect(s, p)

Considering p as another Set we could easily take the intersection and find the filtered result

# Scala Notes – Partial Functions

A partial function is a **unary function** which doesn’t require all the values of the domain. It should be a sub type of PartialFunction trait (PartialFunction).

Syntax example:

val statusMessage: PartialFunction[Int, String] = { case 200 => "Okay" case 400 => "Prob" } statusMessage.isDefinedAt(200) //can be used to check whether the value 200 is included in the domain. statusMessage(400) // Prob statusMessage(100) // Match error

# Lambda Calculus – Notes 1

Lambda calculus consist of 3 things, **variables**, **function abstractions **and **function application**. Alpha renaming and Beta reduction are two operations which we can do on **Lambda terms**

**Example**

lambda x. x

here x is a variable

lambda x. x is a function abstraction

(lambda x. x) y is a function application

since lambda calculus treats lambdas or functions as first class citizens the variable can be another lambda abstraction in a function application, which means (lambda x. lambda y. x + y ) lambda z is a valid lambda application.

**Expressing Natural numbers in lambda calculus**

0 -> lambda f.lambda x. x

this statement means 0 a way of writing zero, how would I put it, a way of expressing nothingness in decimal number system is expressed as lambda abstraction lambda f.lambda x. x in lambda calculus.

1 -> lambda f.lambda x.f(x)

1 in decimal system is represented as a single function application over x in the lambda expression.

2 -> two times the application of function

-> lambda f.lambda x.f(f(x))

we can derive n natural numbers by Peano’s arithmetic

1 -> successor of 0

2 -> successor of 1

so we need a way of representing the function successor (which would be applying one more function on x than its predecessor)

successor -> lambda n.lambda f.lambda x.f(n(f, x))

1 -> successor 0

-> lambda n.lambda f.lambda x.f(n(f, x)) (lambda f.lambda x. x)

-> since the lambda abstraction and variable value contains conflicting variable names

we will do alpha renaming on the value

-> lambda n.lambda f.lambda x.f(n(f, x)) (lambda g.lambda y. y)

-> lambda f.lambda x.f(lambda g.lambda y.y (f, x))

-> lambda f.lambda x.f(x) == 1

now lets define a plus function, successor was just incrementing 1 to its param, in lambda calculus terms it was applying one more function on the inner lambda’s param. In case of addition say we want to add 1 to 2 so we want to get lambda f.lambda x.f(x) + lambda f.lambda x.f(f(x)) = lambda f.lambda x.f(f(f(x)))

so the plus function adds n function application to the second lambda if we want to add n + _.

plus -> lambda n.lambda m.lambda f.lambda x.m(f, n(f, x))

1 plus 2

-> lambda n.lambda m.lambda f.lambda x.m(f, n(f, x)) (lambda f.lambda x.f(x), lambda f.lambda x.f(f(x)))

alpha renaming

-> lambda n.lambda m.lambda f.lambda x.m(f, n(f, x)) (lambda g.lambda y.g(y), lambda h.lambda z.h(h(z)))

-> lambda f.lambda x.lambda g.lambda y.g(y)(f, lambda h.lambda z.h(h(z))(f, x))

-> lambda f.lambda x.lambda g.lambda y.g(y)(f, f(f(x))

-> lambda f.lambda x.f(f(f(x))) == 3

# Tail call optimisation in Scala – Have a look into Java byte code

Let’s see a power function

def power(x:Int, n:Int): Long = { @tailrec def loop(x:Int, n:Int, acc:Long): Long = { if(n >= 2) loop(x, n-1, acc * x) else acc } loop(x, n, x) } power(2, 3)

The power function calculates the value of x^n, this is achieved by a recursive function loop. If a function is tail recursive, which means if the last expression of the function’s body is a recursive call or has recursive calls as leaf nodes (if you consider the AST), then the compiler will optimise the function by converting it to do the execute iteratively (optimisation here is the removal of additional stack frames required while execution). The inner function loop here is a tail recursive call (@tailrec will ensure that the loop method is tail recursive, if the compiler cannot optimise it, it will throw error at compile time).

Inorder to compile the code snippet and see the bytecode lets wrap the function in a class.

Power.scala

import scala.annotation.tailrec /** * Created by sarath on 17/06/17. */ object Power { def power(x:Int, n:Int): Long = { @tailrec def loop(x:Int, n:Int, acc:Long): Long = { if(n >= 2) loop(x, n-1, acc * x) else acc } loop(x, n, x) } def main(args: Array[String]): Unit = { println(power(2, 3)) } }

for a single Power.scala source code the compiler has generated two class files Power.class and Power$.class

Let’s inspect the byte code of Power.class

the power method is where we want to look into

public static long power(int, int); Code: 0: getstatic #16 // Field Power$.MODULE$:LPower$; 3: iload_0 4: iload_1 5: invokevirtual #22 // Method Power$.power:(II)J 8: lreturn

This method is just invoking another power method of same signature inside the Power$.class, so let’s see the byte code of Power$.class

Compiled from "Power.scala" public final class Power$ { public static final Power$ MODULE$; public static {}; Code: 0: new #2 // class Power$ 3: invokespecial #12 // Method "<init>":()V 6: return public long power(int, int); Code: 0: aload_0 1: iload_1 2: iload_2 3: iload_1 4: i2l 5: invokespecial #18 // Method loop$1:(IIJ)J 8: lreturn public void main(java.lang.String[]); Code: 0: getstatic #29 // Field scala/Predef$.MODULE$:Lscala/Predef$; 3: aload_0 4: iconst_2 5: iconst_3 6: invokevirtual #31 // Method power:(II)J 9: invokestatic #37 // Method scala/runtime/BoxesRunTime.boxToLong:(J)Ljava/lang/Long; 12: invokevirtual #41 // Method scala/Predef$.println:(Ljava/lang/Object;)V 15: return private final long loop$1(int, int, long); Code: 0: iload_2 1: iconst_2 2: if_icmplt 19 5: iload_1 6: iload_2 7: iconst_1 8: isub 9: lload_3 10: iload_1 11: i2l 12: lmul 13: lstore_3 14: istore_2 15: istore_1 16: goto 0 19: lload_3 20: lreturn private Power$(); Code: 0: aload_0 1: invokespecial #46 // Method java/lang/Object."<init>":()V 4: aload_0 5: putstatic #48 // Field MODULE$:LPower$; 8: return }

The method public long power(int, int) is the one being invoked from Power.class. Now the interesting part comes.

In the source code we have made use of an inner tail recursive function loop to do the computation, as there is no notion of inner functions in Java, loop has been transformed in to the function private final long loop$1(int, int, long) by the Scala compiler, and this loop$1 is being invoked at line number 32 from power method.

private final long loop$1(int, int, long); Code: 0: iload_2 1: iconst_2 2: if_icmplt 19 5: iload_1 6: iload_2 7: iconst_1 8: isub 9: lload_3 10: iload_1 11: i2l 12: lmul 13: lstore_3 14: istore_2 15: istore_1 16: goto 0 19: lload_3 20: lreturn

at first look one thing is clear there is no self invocation and there is a goto (loops are implemented using jumps in jvm)

Let’s closely inspect the method

iload_2 – pushes the 3rd element from the local variable array to the operand stack

iconst_2 – pushes integer constant 2 to operand stack

if_icmplt 19 – comparison operator less than, if true jump to 19

iload_1 – pushes the 1st element from the local variable array to the operand stack

iload_2 – pushes the 2nd element from the local variable array to the operand stack

iconst_1 – pushes integer constant 1 to operand stack

isub – pops two elements from stack and do subtraction (2nd element of stack – 1st element of stack)

iload_3 – pushes the 3rd element from the local variable array to the operand stack

iload_1 – pushes the 1st element from the local variable array to the operand stack

i2l – pop from op-stack convert to long push it back

lmul – pop last two multiply push the result back

lstore_3 – pop the result and store it back to local variable array pos 3 – mutating the accumulator

istore_2 – pop from stack and store it at pos 2 – mutating n

istore_1 – pop from stack and store it at pos 1 – mutating x

goto 0 – jump to inst 0 – offsetting the execution engine

lload_3 – load the long value from 3rd pos of local variable array

lreturn – return pop the long value from the operand stack and return from the function

so basically they have converted the body of loop as it is and removed the function call by mutating the local variables and using goto to repeatedly execute the body.

Edit: Power function shown here is incorrect, the condition inside loop should be n >= 1 and while calling loop acc should be passed as 1.

# Reflections in Golang

**What is reflection?**

Reflection is the capability provided by a language runtime to structurally analyze the data and code, manipulate and generate the data & code and dynamically direct the execution flow.

**How is reflection possible??**

Inorder to reflect on the data and code the meta data should be available at runtime and the only possibility of this in a compiled language is that the compiler will be baking the required meta information(Example: name and type of data, function names and signature, meta information of the source code etc..) while compiling the code. (Interpreted languages has the AST available at runtime). Once this info is available at runtime, its easy to provide it to the user using the reflection APIs provided by the language.

**Why reflection??**

David John Wheeler, a famous computer scientist has quoted that “All problems in computer science can be solved by adding another level of indirection”. Reflection brings this indirection from a program execution point of view, and there are quite a lot of examples out there in Java and .Net world which builds different levels of abstractions that solves many software design problems using reflection. One example I have seen in Golang is the json encoding and decoding utilities which accept any value and convert it to json and also builds given structures from json (I have just started learning Go).

**An Example in Golang**

I golang reflection is restricted to structural analysis, creation of data (only applicable on settable types) and dynamic method invocation

Refer the api doc for more details on the api.

This is just a demonstration

package main import ( "fmt" "reflect" //"unsafe" ) type Contact struct { id uint32 `myTag1:"asd", myTag2:"ff", das` Name string Phone string } type InterFaceA interface { A() string } type InterFaceB interface { B() string } func (c *Contact) A() string { return fmt.Sprintf("%#v", *c) } func (c Contact) B() string { return fmt.Sprintf("%#v", c) } func main() { StructuralAnalysis() DataCreationAndCodeInvocation() } func StructuralAnalysis() { contactPtr := &Contact{id: 1, Name: "Sarath", Phone: "1233"} //Obtaining reflect.Value on the pointer contactValuePtr := reflect.ValueOf(contactPtr) //Obtaining reflect.Type from the Value //It holds the Type obj of Pointer typPtr := contactValuePtr.Type() fmt.Printf("TypePtrReflect: %#v\n", typPtr) //Obtaining reflect.Type of the real struct //It holds the real structure of the struct typ := typPtr.Elem() fmt.Printf("TypeReflect: %#v\n", typ) //Logging the meta information for i := 0; i < typ.NumField(); i++ { tag := typ.Field(i).Tag //Get FieldByName is also avaialble fieldType := typ.Field(i) fmt.Printf("Field--> %#v\n", fieldType) //Need reflect.Value to access the real values fieldValue := contactValuePtr.Elem().Field(i) //Check for the applicability of 2nd Law of reflection if(fieldValue.CanInterface()) { //2nd Law of reflection Value --> Interface fmt.Printf("Field Value--> %#v\n",fieldValue.Interface()) } else { fmt.Println("Field " + fieldType.Name + " is not exported") } //Reading the annotations on fields fmt.Printf("Tag--> myTag1:%#v, myTag2:%#v, das:%#v\n", tag.Get("myTag1"), tag.Get("myTag2"), "" != tag.Get("das")) } //Logging the methods on ptr for i := 0; i < typPtr.NumMethod(); i++ { fmt.Printf("Methods: %#v\n", typPtr.Method(i)) } //Logging the methods on value for i := 0; i < typ.NumMethod(); i++ { fmt.Printf("Methods: %#v\n", typ.Method(i)) } } func DataCreationAndCodeInvocation() { //Creates a Value out of the given data (Value holds a pointer to the data) contactPtrValue := reflect.New(reflect.TypeOf(Contact{})) //Need to call Elem otherwise the value will work on pointer and which is not what we want //We need to work on the data. (Or in other words we will be reflecting on the pointer if //we are not calling the Elem().) contactValue := contactPtrValue.Elem() //Getting a field and setting the same nameFld := contactValue.FieldByName("Name") nameFld.SetString("Dummy Contact") phoneFld:= contactValue.FieldByName("Phone") phoneFld.SetString("987544") //I know it upfront that this field is not settable //If we dont know whether a field is setttable or not use CanSet always idFld := contactValue.FieldByName("id") canSet := idFld.CanSet() if canSet { idFld.SetUint(100) } else { //The below mentioned code is not possible // Refer: https://groups.google.com/d/msg/golang-nuts/ppGGazd9KXI/iARA-HD8ppwJ //fmt.Println(idFld.Type().Name() + " is not settable") //fmt.Println("Switching to set via pointer") //idPtrMem := unsafe.Pointer(contactValue.UnsafeAddr()) //idPtr := (**uint32)(idPtrMem) //**idPtr = (uint32(100)) //**idPtrMem = (uint32(100)) //fmt.Printf("value cannot be set if not exported\n") } //Getting the Methods binded to the contact value object and invoking it { fmt.Println("Calling InterfaceB->B() on Value") //intrFc := contactValue.Interface() methBOnValue, _ := contactValue.Type().MethodByName("B") //Need to pass the struct instance even though the signature says // none in InterfaceB->B(). That's how the struct is available as the //local variable in methods values := methBOnValue.Func.Call([]reflect.Value{contactValue}) fmt.Printf("%#v\n", values[0].String()) fmt.Println("Calling InterfaceA->A() on Value") methAOnValue, exists := contactValue.Type().MethodByName("A") if exists { values = methAOnValue.Func.Call([]reflect.Value{contactPtrValue}) fmt.Printf("%#v\n", values[0].String()) } else { fmt.Println("InterfaceA->A() doesn't exist on Value obj") } } //Getting the Methods binded to the contact pointer and invoking it { fmt.Println("Calling InterfaceA->A() on Ptr") methAOnPtr, _ := contactPtrValue.Type().MethodByName("A") //Need to pass the struct instance even though the signature says // none in InterfaceB->B(). That's how the struct is available as the //local variable in methods valuesA := methAOnPtr.Func.Call([]reflect.Value{contactPtrValue}) fmt.Printf("%#v\n", valuesA[0].String()) fmt.Println("Calling InterfaceB->B() on Ptr") methBOnPtr, _ := contactPtrValue.Type().MethodByName("B") //Need to pass the struct instance even though the signature says // none in InterfaceB->B(). That's how the struct is available as the //local variable in methods valuesB := methBOnPtr.Func.Call([]reflect.Value{contactPtrValue}) fmt.Printf("%#v\n", valuesB[0].String()) } }