{"id":808,"date":"2026-10-08T09:00:00","date_gmt":"2026-10-08T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/"},"modified":"2026-11-29T01:29:32","modified_gmt":"2026-11-28T19:59:32","slug":"go-interface-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/","title":{"rendered":"Go Interface Programs: Shapes, Stringer, Type Switch and More"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go interface programs let different types share behaviour, so one function can work with a circle, a rectangle or any shape you add later. Interfaces are how Go does polymorphism, and they power key parts of the standard library such as <code>io.Writer<\/code>, <code>fmt.Stringer<\/code> and <code>error<\/code>. In this post, you will write fourteen interface programs with their exact output, and then practice the interface questions that trip up many developers, including the famous nil interface trap.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You need structs and methods from our <a href=\"https:\/\/smitabharat.com\/blog\/go-struct-programs\/\">Go struct programs<\/a>, and pointer receivers from our <a href=\"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/\">Go pointer programs<\/a>. For the theory, read <a href=\"https:\/\/smitabharat.com\/blog\/go-interfaces-examples\/\">Go interfaces explained with examples<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Interface Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go interface programs define an interface, which is a list of method signatures. Any type that has all those methods automatically satisfies the interface. You never write &#8220;implements&#8221;, because Go checks it for you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a phone charger port. The charger does not care which brand of phone you plug in. It only cares that the phone has the right port. An interface is that port: any type with the right methods fits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Interface Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interfaces let you write code that works with types that do not exist yet. For example, a payment service can accept any payment method, a logger can write to a file or the screen, and tests can swap a real database for a fake one.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Flexible code:<\/strong> one function works with many types.<\/li>\n\n\n<li><strong>Testing:<\/strong> pass a fake that satisfies the same interface.<\/li>\n\n\n<li><strong>Standard library:<\/strong> <code>io.Reader<\/code>, <code>io.Writer<\/code>, <code>error<\/code> and <code>fmt.Stringer<\/code> are all interfaces.<\/li>\n\n\n<li><strong>Interviews:<\/strong> nil interfaces, type assertions and pointer receivers are common questions.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Interface Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An interface value holds two things: the concrete type and the value of that type. When you call a method on the interface, Go looks up the method for the stored type and calls it. If the type has every method in the interface, Go allows the assignment at compile time.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-1-1024x640.png\" alt=\"Go interface programs diagram showing Circle and Rectangle satisfying a Shape interface\" class=\"wp-image-792\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Any type with an Area method satisfies Shape, so one function can work with every shape.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the interface tools used in this post.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Tool<\/th><th>Syntax<\/th><th>Use it for<\/th><\/tr><\/thead><tbody><tr><td>Interface type<\/td><td>type Shape interface { Area() float64 }<\/td><td>Define behaviour<\/td><\/tr><tr><td>Satisfy<\/td><td>Add the methods to your type<\/td><td>No keyword needed<\/td><\/tr><tr><td>Type assertion<\/td><td>c, ok := s.(Circle)<\/td><td>Get the concrete type<\/td><\/tr><tr><td>Type switch<\/td><td>switch v := s.(type) { }<\/td><td>Handle many types<\/td><\/tr><tr><td>Empty interface<\/td><td>any (same as interface{})<\/td><td>Hold any value<\/td><\/tr><tr><td>Embedding<\/td><td>type ReadWriter interface { Reader; Writer }<\/td><td>Combine interfaces<\/td><\/tr><tr><td>Compile check<\/td><td>var _ Shape = Circle{}<\/td><td>Prove a type fits<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Interface Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go interface programs start with a tiny example and then build the classic shapes example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Your First Interface with Two Vehicles<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Start here if Go interface programs are new to you. The interface has one method, and two simple types provide it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Vehicle is anything that can tell us how many wheels it has.\ntype Vehicle interface {\n    Wheels() int\n}\n\n\/\/ Bike is one kind of vehicle.\ntype Bike struct{}\n\n\/\/ Car is another kind of vehicle.\ntype Car struct{}\n\n\/\/ Wheels makes Bike satisfy Vehicle.\nfunc (bike Bike) Wheels() int { return 2 }\n\n\/\/ Wheels makes Car satisfy Vehicle.\nfunc (car Car) Wheels() int { return 4 }\n\n\/\/ showWheels works with ANY Vehicle.\nfunc showWheels(name string, vehicle Vehicle) {\n    fmt.Println(name, \"has\", vehicle.Wheels(), \"wheels\")\n}\n\nfunc main() {\n    showWheels(\"Bike\", Bike{})\n    showWheels(\"Car\", Car{})\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Bike has 2 wheels\nCar has 4 wheels<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Vehicle<\/code> only asks for one method, <code>Wheels() int<\/code>. Both <code>Bike<\/code> and <code>Car<\/code> have that method, so both count as a <code>Vehicle<\/code>. Then <code>showWheels<\/code> can take either one without knowing which it got.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Shape Interface with Area<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This program uses the names from the diagram above: <code>Shape<\/code>, <code>Circle<\/code>, <code>Rectangle<\/code> and <code>Area<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"math\"\n)\n\n\/\/ Shape is any type that can work out its own area.\ntype Shape interface {\n    Area() float64\n}\n\n\/\/ Rectangle has a width and a height.\ntype Rectangle struct{ Width, Height float64 }\n\n\/\/ Circle has a radius.\ntype Circle struct{ Radius float64 }\n\n\/\/ Area of a rectangle is width times height.\nfunc (rectangle Rectangle) Area() float64 { return rectangle.Width * rectangle.Height }\n\n\/\/ Area of a circle is pi times radius squared.\nfunc (circle Circle) Area() float64 { return math.Pi * circle.Radius * circle.Radius }\n\n\/\/ printArea accepts any Shape and prints its type and area.\nfunc printArea(shape Shape) {\n    fmt.Printf(\"%T area: %.2f\\n\", shape, shape.Area()) \/\/ %T prints the type name\n}\n\nfunc main() {\n    printArea(Rectangle{4, 5})\n    printArea(Circle{3})\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>main.Rectangle area: 20.00\nmain.Circle area: 28.27<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>printArea<\/code> accepts any <code>Shape<\/code>. Both types satisfy it, because each one has an <code>Area() float64<\/code> method. Inside, Go calls the right <code>Area<\/code> for the type you passed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: A Slice of Interfaces<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Shape is any type with an Area method.\ntype Shape interface{ Area() float64 }\n\n\/\/ Square has one side length.\ntype Square struct{ Side float64 }\n\n\/\/ Triangle has a base and a height.\ntype Triangle struct{ Base, Height float64 }\n\nfunc (square Square) Area() float64     { return square.Side * square.Side }\nfunc (triangle Triangle) Area() float64 { return 0.5 * triangle.Base * triangle.Height }\n\nfunc main() {\n    shapes := []Shape{Square{3}, Triangle{4, 6}, Square{2}} \/\/ different types in one slice\n    total := 0.0\n    for _, shape := range shapes {\n        total += shape.Area()\n    }\n    fmt.Println(\"Total area:\", total)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Total area: 25<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So the slice holds different types, but the loop treats them all the same way. It adds 9, 12 and 4 to get 25. Adding a new shape needs no change to this loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Payment Methods<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Payer is anything that can pay an amount.\ntype Payer interface {\n    Pay(amount float64) string\n}\n\n\/\/ UPI pays with a UPI ID.\ntype UPI struct{ ID string }\n\n\/\/ Card pays with a card.\ntype Card struct{ Last4 string }\n\nfunc (upi UPI) Pay(amount float64) string {\n    return fmt.Sprintf(\"Paid %.2f via UPI %s\", amount, upi.ID)\n}\n\nfunc (card Card) Pay(amount float64) string {\n    return fmt.Sprintf(\"Paid %.2f via card ending %s\", amount, card.Last4)\n}\n\n\/\/ checkout works with any Payer.\nfunc checkout(payer Payer, total float64) {\n    fmt.Println(payer.Pay(total))\n}\n\nfunc main() {\n    checkout(UPI{\"asha@bank\"}, 499)\n    checkout(Card{\"4242\"}, 1250.5)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Paid 499.00 via UPI asha@bank\nPaid 1250.50 via card ending 4242<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each type has its own <code>Pay<\/code> method with its own message. As a result, <code>checkout<\/code> does not know or care how each method pays. Tomorrow you can add a <code>Wallet<\/code> type without touching <code>checkout<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Standard Library Go Interface Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go&#8217;s standard library is full of small interfaces. These Go interface programs use the most common ones.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: fmt.Stringer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Temperature is a float64 with its own String method.\ntype Temperature float64\n\n\/\/ String tells fmt how to print a Temperature.\nfunc (temperature Temperature) String() string {\n    return fmt.Sprintf(\"%.1f\u00b0C\", float64(temperature))\n}\n\nfunc main() {\n    var today Temperature = 36.6\n    fmt.Println(today)                \/\/ fmt calls String() for you\n    fmt.Printf(\"Today: %v\\n\", today) \/\/ %v also uses String()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>36.6\u00b0C\nToday: 36.6\u00b0C<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Any type with <code>String() string<\/code> satisfies <code>fmt.Stringer<\/code>. The <code>fmt<\/code> package checks for it and calls it when printing. That is why you see the \u00b0C sign without asking for it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: A Custom error Type<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>error<\/code> is just an interface with one method: <code>Error() string<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ StockError says how many of an item are left.\ntype StockError struct {\n    Item string\n    Have int\n}\n\n\/\/ Error makes StockError satisfy the error interface.\nfunc (stockError StockError) Error() string {\n    return fmt.Sprintf(\"only %d %s left\", stockError.Have, stockError.Item)\n}\n\n\/\/ buy returns an error when there is not enough stock.\nfunc buy(item string, quantity, stock int) error {\n    if quantity &gt; stock {\n        return StockError{item, stock}\n    }\n    return nil \/\/ nil means \"no error\"\n}\n\nfunc main() {\n    fmt.Println(buy(\"pen\", 2, 10))\n    fmt.Println(buy(\"bag\", 5, 3))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;nil&gt;\nonly 3 bag left<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>StockError<\/code> has an <code>Error() string<\/code> method, so you can return it as an <code>error<\/code>. The first call has enough stock and returns nil. We go deeper into custom errors in the next post of this level.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: io.Writer Sends Output Anywhere<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"io\"\n    \"os\"\n    \"strings\"\n)\n\n\/\/ report writes one line to any io.Writer.\nfunc report(writer io.Writer, name string, score int) {\n    fmt.Fprintf(writer, \"%s scored %d\\n\", name, score) \/\/ Fprintf prints to a writer instead of the screen\n}\n\nfunc main() {\n    report(os.Stdout, \"Ravi\", 88) \/\/ os.Stdout is the screen\n\n    var builder strings.Builder \/\/ a builder collects text in memory\n    report(&amp;builder, \"Meera\", 95)\n    fmt.Print(\"Saved: \", builder.String())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Ravi scored 88\nSaved: Meera scored 95<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>report<\/code> writes to any <code>io.Writer<\/code>. Here it writes once to the screen and once to a string builder. It could also write to a file or a network connection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Implement sort.Interface<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\n\/\/ ByLength is a list of words that sorts by word length.\ntype ByLength []string\n\n\/\/ Len returns how many words there are.\nfunc (words ByLength) Len() int { return len(words) }\n\n\/\/ Less says if word i is shorter than word j.\nfunc (words ByLength) Less(i, j int) bool { return len(words[i]) &lt; len(words[j]) }\n\n\/\/ Swap swaps two words (a plain swap of two values).\nfunc (words ByLength) Swap(i, j int) { words[i], words[j] = words[j], words[i] }\n\nfunc main() {\n    words := []string{\"banana\", \"fig\", \"apple\", \"kiwi\"}\n    sort.Sort(ByLength(words)) \/\/ convert to ByLength so sort can use our methods\n    fmt.Println(words)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[fig kiwi apple banana]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sort.Sort<\/code> works with any type that has <code>Len<\/code>, <code>Less<\/code> and <code>Swap<\/code>. It calls those three methods again and again until the list is in order. New code often uses <code>slices.SortFunc<\/code> instead, as in our <a href=\"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/\">Go sorting programs<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Type Checks in Go Interface Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes Go interface programs need the real type stored inside an interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: The Empty Interface any Holds Any Value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before Go interface programs check types, it helps to see what <code>any<\/code> can hold.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var box any \/\/ any can hold a value of any type\n\n    box = 10\n    fmt.Printf(\"%v is a %T\\n\", box, box) \/\/ %v prints the value, %T prints the type\n\n    box = \"hello\"\n    fmt.Printf(\"%v is a %T\\n\", box, box)\n\n    box = true\n    fmt.Printf(\"%v is a %T\\n\", box, box)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>10 is a int\nhello is a string\ntrue is a bool<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>any<\/code> is an interface with no methods, so every type satisfies it. The same <code>box<\/code> variable holds an int, then a string, then a bool. <code>%T<\/code> shows which type is inside at each step.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Type Assertion with ok<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Shape is any type with an Area method.\ntype Shape interface{ Area() float64 }\n\n\/\/ Circle has a radius.\ntype Circle struct{ Radius float64 }\n\n\/\/ Square has one side length.\ntype Square struct{ Side float64 }\n\nfunc (circle Circle) Area() float64 { return 3.14 * circle.Radius * circle.Radius }\nfunc (square Square) Area() float64 { return square.Side * square.Side }\n\nfunc main() {\n    var shape Shape = Circle{2}\n\n    \/\/ ok is true only if shape really holds a Circle.\n    if circle, ok := shape.(Circle); ok {\n        fmt.Println(\"circle with radius\", circle.Radius)\n    }\n\n    _, ok := shape.(Square) \/\/ _ ignores the value, we only want ok\n    fmt.Println(\"is square:\", ok)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>circle with radius 2\nis square: false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>shape.(Circle)<\/code> asks &#8220;is a Circle inside?&#8221;. The two-value form never panics. If you wrote <code>shape.(Square)<\/code> without <code>ok<\/code>, the program would panic because <code>shape<\/code> holds a <code>Circle<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Type Switch on any<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ describe returns a short text for each kind of value.\nfunc describe(value any) string {\n    switch typed := value.(type) { \/\/ typed has the real type in each case\n    case int:\n        return fmt.Sprintf(\"int %d\", typed)\n    case string:\n        return fmt.Sprintf(\"string %q\", typed)\n    case []int:\n        return fmt.Sprintf(\"slice of %d ints\", len(typed))\n    case error:\n        return \"error: \" + typed.Error()\n    case nil:\n        return \"nil\"\n    default:\n        return fmt.Sprintf(\"other %T\", typed)\n    }\n}\n\nfunc main() {\n    values := []any{42, \"go\", []int{1, 2}, fmt.Errorf(\"oops\"), nil, 3.5}\n    for _, value := range values {\n        fmt.Println(describe(value))\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>int 42\nstring \"go\"\nslice of 2 ints\nerror: oops\nnil\nother float64<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A type switch checks the real type inside <code>value<\/code>, one case at a time. In each case, <code>typed<\/code> already has that type, so you can use it directly. A case can name an interface too, like <code>error<\/code>. See our <a href=\"https:\/\/smitabharat.com\/blog\/go-type-assertion-type-switch\/\">type assertion and type switch guide<\/a> for more.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Interface Embedding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Reader can read text.\ntype Reader interface{ Read() string }\n\n\/\/ Writer can write text.\ntype Writer interface{ Write(text string) }\n\n\/\/ ReadWriter needs both methods.\ntype ReadWriter interface {\n    Reader\n    Writer\n}\n\n\/\/ Notebook stores pages of text.\ntype Notebook struct{ pages []string }\n\n\/\/ Write adds a page (pointer receiver, so it changes the notebook).\nfunc (notebook *Notebook) Write(text string) { notebook.pages = append(notebook.pages, text) }\n\n\/\/ Read returns all pages as one string.\nfunc (notebook *Notebook) Read() string { return fmt.Sprint(notebook.pages) }\n\nfunc main() {\n    var readWriter ReadWriter = &amp;Notebook{} \/\/ &amp; because the methods use *Notebook\n    readWriter.Write(\"page one\")\n    readWriter.Write(\"page two\")\n    fmt.Println(readWriter.Read())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[page one page two]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ReadWriter<\/code> combines two smaller interfaces. <code>*Notebook<\/code> has both methods, so it fits. This is exactly how <code>io.ReadWriter<\/code> is built in the standard library.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Compile-Time Interface Check<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Notifier can send a message.\ntype Notifier interface{ Notify(message string) string }\n\n\/\/ Email sends to one address.\ntype Email struct{ To string }\n\n\/\/ Notify makes Email satisfy Notifier.\nfunc (email Email) Notify(message string) string {\n    return \"email to \" + email.To + \": \" + message\n}\n\n\/\/ Fails to compile if Email stops satisfying Notifier.\nvar _ Notifier = Email{}\n\nfunc main() {\n    fmt.Println(Email{\"asha@example.com\"}.Notify(\"Order shipped\"))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>email to asha@example.com: Order shipped<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The line <code>var _ Notifier = Email{}<\/code> asks the compiler to check the fit. Also, the blank variable costs nothing at run time. It turns a missing method into a clear compile error, right next to the type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: A Fake for Testing<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Store can look up the price of an item.\ntype Store interface {\n    Price(item string) (int, bool)\n}\n\n\/\/ FakeStore is a simple map that acts like a store in tests.\ntype FakeStore map[string]int\n\n\/\/ Price returns the price and true, or 0 and false if the item is missing.\nfunc (store FakeStore) Price(item string) (int, bool) {\n    price, ok := store[item] \/\/ ok is true if the item is in the map\n    return price, ok\n}\n\n\/\/ cartTotal adds up the prices of all known items.\nfunc cartTotal(store Store, items []string) int {\n    total := 0\n    for _, item := range items {\n        price, ok := store.Price(item)\n        if ok {\n            total += price\n        }\n    }\n    return total\n}\n\nfunc main() {\n    fake := FakeStore{\"pen\": 10, \"book\": 250}\n    fmt.Println(cartTotal(fake, []string{\"pen\", \"book\", \"pen\", \"lamp\"}))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>270<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>cartTotal<\/code> only needs something with a <code>Price<\/code> method. The fake map gives 10 + 250 + 10 and skips the unknown lamp. In real code, <code>Store<\/code> would talk to a database, and our <a href=\"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/\">Go mocking with interfaces guide<\/a> shows this in full.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Interface vs Concrete Type in Go Interface Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interfaces are powerful, but Go interface programs should not use them everywhere. The diagram and table below compare them with concrete types.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-2-1024x640.png\" alt=\"Go interface programs comparison of interfaces and concrete types\" class=\"wp-image-793\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Use an interface when callers need flexibility, and a concrete type when there is only one implementation.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Interface parameter<\/th><th>Concrete type parameter<\/th><\/tr><\/thead><tbody><tr><td>Accepts<\/td><td>Any type with the methods<\/td><td>Only that type<\/td><\/tr><tr><td>Easy to fake in tests<\/td><td>Yes<\/td><td>Harder<\/td><\/tr><tr><td>Speed<\/td><td>Tiny cost per call<\/td><td>Fastest<\/td><\/tr><tr><td>Fields visible<\/td><td>No, only methods<\/td><td>Yes<\/td><\/tr><tr><td>Best for<\/td><td>Plug-in behaviour, tests<\/td><td>One fixed implementation<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A Go saying sums it up: accept interfaces, return concrete types. Keep interfaces small, often just one or two methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Interface Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Pointer receiver, value assignment:<\/strong> if <code>Area<\/code> has a <code>*Circle<\/code> receiver, a plain <code>Circle<\/code> does not satisfy <code>Shape<\/code>.<\/li>\n\n\n<li><strong>The nil interface trap:<\/strong> an interface holding a nil pointer is not nil, so <code>err != nil<\/code> can be true.<\/li>\n\n\n<li><strong>Assertion without ok:<\/strong> <code>s.(Square)<\/code> panics when the type is wrong. Use the two-value form.<\/li>\n\n\n<li><strong>Huge interfaces:<\/strong> big interfaces are hard to satisfy and fake. Split them.<\/li>\n\n\n<li><strong>Interfaces with one implementation:<\/strong> add an interface only when you need a second one, like a test fake.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Interface Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Keep interfaces small<\/strong>, like <code>io.Reader<\/code> with one method.<\/li>\n\n\n<li><strong>Define interfaces where they are used<\/strong>, not next to the type that implements them.<\/li>\n\n\n<li><strong>Use the two-value type assertion<\/strong> whenever the type might differ.<\/li>\n\n\n<li><strong>Add a compile-time check<\/strong> like <code>var _ Shape = Circle{}<\/code> for important types.<\/li>\n\n\n<li><strong>Return concrete types<\/strong> from constructors, so callers keep full access.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Interface Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers use short Go interface programs to test the trickiest rules of interfaces. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Satisfaction Questions in Go Interface Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. How does a type implement an interface in Go?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Implicitly. If a type has every method in the interface, with matching names and signatures, it satisfies the interface. There is no <code>implements<\/code> keyword.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. Why does this program not compile?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\n\/\/ Shape is any type with an Area method.\ntype Shape interface{ Area() float64 }\n\n\/\/ Circle has a radius.\ntype Circle struct{ Radius float64 }\n\n\/\/ Area uses a pointer receiver, so only *Circle has this method.\nfunc (circle *Circle) Area() float64 { return 3.14 * circle.Radius * circle.Radius }\n\nfunc main() {\n    var shape Shape = Circle{2} \/\/ a plain Circle value, not a pointer\n    _ = shape\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Circle does not implement Shape (method Area has pointer receiver)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Only <code>*Circle<\/code> has the <code>Area<\/code> method, so you must write <code>&amp;Circle{2}<\/code>. A value type&#8217;s method set does not include pointer receiver methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What is the difference between any and interface{}?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is no difference. <code>any<\/code> is an alias for <code>interface{}<\/code>, added in Go 1.18 to make code easier to read.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Nil and Comparison Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ MyError is a custom error type.\ntype MyError struct{}\n\nfunc (*MyError) Error() string { return \"my error\" }\n\n\/\/ find returns an error interface that holds a *MyError pointer.\nfunc find(fail bool) error {\n    var errorPointer *MyError \/\/ starts as a nil pointer\n    if fail {\n        errorPointer = &amp;MyError{}\n    }\n    return errorPointer \/\/ even when nil, the interface remembers the type\n}\n\nfunc main() {\n    err := find(false) \/\/ err is the error value\n    fmt.Println(err == nil)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the famous nil interface trap. The function returns an interface holding a nil <code>*MyError<\/code>. The interface has a type, so it is not nil. Fix it by returning a plain <code>nil<\/code> when there is no error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var empty any       \/\/ holds nothing yet\n    var first any = 5   \/\/ holds the int 5\n    var second any = 5  \/\/ also holds the int 5\n    fmt.Println(empty == nil, first == second, first == 5)\n    fmt.Printf(\"%v %T\\n\", empty, first)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true true true\n&lt;nil&gt; int<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Interfaces compare equal when they hold the same type and the same value. An empty interface with nothing inside is nil.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var first any = []int{1}\n    var second any = []int{1}\n\n    \/\/ defer runs this function at the end, and recover catches the panic.\n    defer func() {\n        fmt.Println(\"recovered:\", recover())\n    }()\n\n    fmt.Println(first == second) \/\/ slices cannot be compared, so this panics\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>recovered: runtime error: comparing uncomparable type []int<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The code compiles, because both are <code>any<\/code>. But at run time, Go cannot compare slices, so it panics. We used <code>recover<\/code> to show the message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Assertion and Output Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. If a type assertion fails without ok, what happens?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var value any = \"hello\"\n    number := value.(int) \/\/ value holds a string, not an int\n    fmt.Println(number)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>panic: interface conversion: interface {} is string, not int<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The assertion says &#8220;I am sure this is an int&#8221;, but it is a string. So Go panics and stops the program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Animal is anything that makes a sound.\ntype Animal interface{ Sound() string }\ntype Dog struct{}\ntype Cat struct{}\n\nfunc (Dog) Sound() string { return \"Woof\" }\nfunc (Cat) Sound() string { return \"Meow\" }\n\nfunc main() {\n    animals := map[string]Animal{\"dog\": Dog{}, \"cat\": Cat{}} \/\/ map values are interfaces\n    fmt.Println(animals[\"cat\"].Sound(), animals[\"dog\"].Sound())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Meow Woof<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The map stores both animals as <code>Animal<\/code> values. Each lookup calls the <code>Sound<\/code> method of the real type inside.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"strings\"\n)\n\n\/\/ Upper prints its text in capital letters.\ntype Upper struct{ text string }\n\nfunc (upper Upper) String() string { return strings.ToUpper(upper.text) }\n\nfunc main() {\n    shout := Upper{\"go is fun\"}\n    fmt.Println(shout)\n    fmt.Printf(\"%s | %v | %+v\\n\", shout, shout, shout)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>GO IS FUN\nGO IS FUN | GO IS FUN | GO IS FUN<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once a type has a <code>String<\/code> method, <code>fmt<\/code> uses it for <code>%s<\/code>, <code>%v<\/code> and <code>%+v<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. Can an interface hold a pointer?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. An interface can hold any type that satisfies it, including pointers. In fact, types with pointer receiver methods are usually stored as pointers in interfaces.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Speaker can say something.\ntype Speaker interface{ Speak() string }\n\n\/\/ Robot has a name.\ntype Robot struct{ Name string }\n\nfunc (robot Robot) Speak() string { return \"I am \" + robot.Name }\n\nfunc main() {\n    var speaker Speaker = Robot{\"R2\"}\n    robot := speaker.(Robot) \/\/ robot is a copy of the Robot inside\n    robot.Name = \"C3\"        \/\/ changes only the copy\n    fmt.Println(speaker.Speak(), \"|\", robot.Speak())\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>I am R2 | I am C3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The assertion gives you a copy of the struct. Changing the copy does not change the value inside the interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. Why should interfaces be small?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Small interfaces are easy to satisfy, easy to fake in tests and easy to combine. <code>io.Reader<\/code> has one method, yet hundreds of types in the standard library and beyond satisfy it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task is one of the most practical Go interface programs. First, create a <code>Notifier<\/code> interface with <code>Send(to, msg string) error<\/code>, and write <code>SMS<\/code> and <code>Email<\/code> types that satisfy it. Write a function that sends the same message through a slice of notifiers. Hint: return an error from <code>SMS<\/code> when the number is shorter than 10 digits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/go.dev\/tour\/methods\/9\">A Tour of Go: Interfaces<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/effective_go#interfaces\">Effective Go: interfaces<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/faq#nil_error\">Go FAQ: why is my nil error value not equal to nil<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go interface programs define behaviour as a set of methods, and types satisfy them implicitly.<\/li>\n\n\n<li>An interface value holds a type and a value, and it is nil only when both are empty.<\/li>\n\n\n<li>Use <code>v, ok := x.(T)<\/code> or a type switch to get the concrete type safely.<\/li>\n\n\n<li>Pointer receiver methods only belong to the pointer type, so store <code>&amp;T<\/code> in the interface.<\/li>\n\n\n<li>Keep interfaces small, define them where they are used, and return concrete types.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we look at the most used interface in Go. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-error-handling-programs\/\">Go error handling programs with output<\/a>, with custom errors, wrapping, errors.Is, errors.As and recover. For interview practice, see the <a href=\"https:\/\/smitabharat.com\/blog\/go-interfaces-interview-questions-part-8\/\">Go interfaces interview questions<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go interface programs let different types share behaviour, so one function can work with a circle, a rectangle or any shape you add later.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":794,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,33,95,11,104],"swapz_series":[],"class_list":["post-808","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interfaces","tag-go-interview-questions","tag-go-lang","tag-go-programs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Interface Programs: Shapes, Stringer, Switch<\/title>\n<meta name=\"description\" content=\"Go interface programs with output: shapes, payments, Stringer, io.Writer, type assertions and switches, plus the nil interface trap explained.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-interface-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Interface Programs: Shapes, Stringer, Switch\" \/>\n<meta property=\"og:description\" content=\"Go interface programs with output: shapes, payments, Stringer, io.Writer, type assertions and switches, plus the nil interface trap explained.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-interface-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-08T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Interface Programs: Shapes, Stringer, Type Switch and More\",\"datePublished\":\"2026-10-08T03:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/\"},\"wordCount\":1837,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-interface-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interfaces\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/\",\"name\":\"Go Interface Programs: Shapes, Stringer, Switch\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-interface-programs-featured.png\",\"datePublished\":\"2026-10-08T03:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:32+00:00\",\"description\":\"Go interface programs with output: shapes, payments, Stringer, io.Writer, type assertions and switches, plus the nil interface trap explained.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-interface-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-interface-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Interface Programs featured image: shape area and payment output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-interface-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Interface Programs: Shapes, Stringer, Type Switch and More\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Interface Programs: Shapes, Stringer, Switch","description":"Go interface programs with output: shapes, payments, Stringer, io.Writer, type assertions and switches, plus the nil interface trap explained.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Interface Programs: Shapes, Stringer, Switch","og_description":"Go interface programs with output: shapes, payments, Stringer, io.Writer, type assertions and switches, plus the nil interface trap explained.","og_url":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-08T03:30:00+00:00","article_modified_time":"2026-11-28T19:59:32+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Interface Programs: Shapes, Stringer, Type Switch and More","datePublished":"2026-10-08T03:30:00+00:00","dateModified":"2026-11-28T19:59:32+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/"},"wordCount":1837,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-featured.png","keywords":["Beginner","Go Interfaces","Go Interview Questions","Go Lang","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/","name":"Go Interface Programs: Shapes, Stringer, Switch","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-featured.png","datePublished":"2026-10-08T03:30:00+00:00","dateModified":"2026-11-28T19:59:32+00:00","description":"Go interface programs with output: shapes, payments, Stringer, io.Writer, type assertions and switches, plus the nil interface trap explained.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-interface-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-interface-programs-featured.png","width":2400,"height":1260,"caption":"Go Interface Programs featured image: shape area and payment output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-interface-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Interface Programs: Shapes, Stringer, Type Switch and More"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=808"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/808\/revisions"}],"predecessor-version":[{"id":889,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/808\/revisions\/889"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/794"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=808"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}