<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GoLang Interfaces. Understanding and utilizing interfaces in GoLang is key to writing reusable Archives -</title>
	<atom:link href="https://irislogic.com/tag/golang-interfaces-understanding-and-utilizing-interfaces-in-golang-is-key-to-writing-reusable/feed/" rel="self" type="application/rss+xml" />
	<link>https://irislogic.com/tag/golang-interfaces-understanding-and-utilizing-interfaces-in-golang-is-key-to-writing-reusable/</link>
	<description></description>
	<lastBuildDate>Thu, 17 Jul 2025 07:07:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://irislogic.com/wp-content/uploads/2024/07/cropped-2-32x32.png</url>
	<title>GoLang Interfaces. Understanding and utilizing interfaces in GoLang is key to writing reusable Archives -</title>
	<link>https://irislogic.com/tag/golang-interfaces-understanding-and-utilizing-interfaces-in-golang-is-key-to-writing-reusable/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>GoLang Interfaces</title>
		<link>https://irislogic.com/golang-interfaces/</link>
		
		<dc:creator><![CDATA[Irislogic]]></dc:creator>
		<pubDate>Fri, 27 Sep 2024 11:13:15 +0000</pubDate>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Trends]]></category>
		<category><![CDATA[and decoupled code.]]></category>
		<category><![CDATA[flexible]]></category>
		<category><![CDATA[GoLang Interfaces. Understanding and utilizing interfaces in GoLang is key to writing reusable]]></category>
		<guid isPermaLink="false">https://irislogic.com/?p=1792</guid>

					<description><![CDATA[<p>1. What are Interfaces in GoLang? Interfaces in GoLang define a set of method signatures (behaviors) that types must implement. [&#8230;]</p>
<p>The post <a href="https://irislogic.com/golang-interfaces/">GoLang Interfaces</a> appeared first on <a href="https://irislogic.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-uagb-image uagb-block-2c829ff0 wp-block-uagb-image--layout-default wp-block-uagb-image--effect-static wp-block-uagb-image--align-none"><figure class="wp-block-uagb-image__figure"><img decoding="async" srcset="https://irislogic.com/wp-content/uploads/2024/09/GoLang-Interfaces-lesson-7-1024x576.png ,https://irislogic.com/wp-content/uploads/2024/09/GoLang-Interfaces-lesson-7.png 780w, https://irislogic.com/wp-content/uploads/2024/09/GoLang-Interfaces-lesson-7.png 360w" sizes="auto, (max-width: 480px) 150px" src="https://irislogic.com/wp-content/uploads/2024/09/GoLang-Interfaces-lesson-7-1024x576.png" alt="interfaces" class="uag-image-1964" width="1024" height="576" title="GoLang Interfaces lesson 7" loading="lazy" role="img"/></figure></div>



<p></p>



<p><strong>1. What are Interfaces in GoLang?</strong> Interfaces in GoLang define a set of method signatures (behaviors) that types must implement. An interface allows different types to be treated as one. Unlike other languages where you explicitly declare that a type implements an interface, in Go, if a type implements all the methods of an interface, it is implicitly considered as implementing that interface.</p>



<p>Here’s a basic example of an interface:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#181c2a;color:#aaafcf">Go</span><span role="button" tabindex="0" data-code="type Speaker interface {
    Speak() string
}" style="color:#babed8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki material-theme-ocean" style="background-color: #0F111A" tabindex="0"><code><span class="line"><span style="color: #89DDFF">type</span><span style="color: #BABED8"> </span><span style="color: #FFCB6B">Speaker</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">interface</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    </span><span style="color: #82AAFF">Speak</span><span style="color: #89DDFF">()</span><span style="color: #BABED8"> </span><span style="color: #C792EA">string</span></span>
<span class="line"><span style="color: #89DDFF">}</span></span></code></pre></div>



<p></p>



<p>Any type that implements the <code>Speak</code> method can be considered a <code>Speaker</code>.</p>



<p><strong>2. Implementing Interfaces</strong> A type implements an interface simply by having methods that match the interface&#8217;s method signatures. There&#8217;s no need to explicitly declare that a type implements an interface.</p>



<p>For example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#181c2a;color:#aaafcf">Go</span><span role="button" tabindex="0" data-code="type Dog struct{}

func (d Dog) Speak() string {
    return &quot;Woof!&quot;
}

type Cat struct{}

func (c Cat) Speak() string {
    return &quot;Meow!&quot;
}" style="color:#babed8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki material-theme-ocean" style="background-color: #0F111A" tabindex="0"><code><span class="line"><span style="color: #89DDFF">type</span><span style="color: #BABED8"> </span><span style="color: #FFCB6B">Dog</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">struct{}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #89DDFF">func</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">(</span><span style="color: #BABED8">d Dog</span><span style="color: #89DDFF">)</span><span style="color: #BABED8"> </span><span style="color: #82AAFF">Speak</span><span style="color: #89DDFF">()</span><span style="color: #BABED8"> </span><span style="color: #C792EA">string</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    </span><span style="color: #89DDFF; font-style: italic">return</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">Woof!</span><span style="color: #89DDFF">&quot;</span></span>
<span class="line"><span style="color: #89DDFF">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #89DDFF">type</span><span style="color: #BABED8"> </span><span style="color: #FFCB6B">Cat</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">struct{}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #89DDFF">func</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">(</span><span style="color: #BABED8">c Cat</span><span style="color: #89DDFF">)</span><span style="color: #BABED8"> </span><span style="color: #82AAFF">Speak</span><span style="color: #89DDFF">()</span><span style="color: #BABED8"> </span><span style="color: #C792EA">string</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    </span><span style="color: #89DDFF; font-style: italic">return</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">Meow!</span><span style="color: #89DDFF">&quot;</span></span>
<span class="line"><span style="color: #89DDFF">}</span></span></code></pre></div>



<p></p>



<p>Both <code>Dog</code> and <code>Cat</code> implement the <code>Speak</code> method, so they implicitly satisfy the <code>Speaker</code> interface.</p>



<p>You can now write a function that accepts the <code>Speaker</code> interface as a parameter:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#181c2a;color:#aaafcf">Go</span><span role="button" tabindex="0" data-code="func MakeItSpeak(s Speaker) {
    fmt.Println(s.Speak())
}
" style="color:#babed8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki material-theme-ocean" style="background-color: #0F111A" tabindex="0"><code><span class="line"><span style="color: #89DDFF">func</span><span style="color: #BABED8"> </span><span style="color: #82AAFF">MakeItSpeak</span><span style="color: #89DDFF">(</span><span style="color: #BABED8">s Speaker</span><span style="color: #89DDFF">)</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    fmt</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Println</span><span style="color: #89DDFF">(</span><span style="color: #BABED8">s</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Speak</span><span style="color: #89DDFF">())</span></span>
<span class="line"><span style="color: #89DDFF">}</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p>You can call this function with any type that implements the <code>Speaker</code> interface:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#181c2a;color:#aaafcf">Go</span><span role="button" tabindex="0" data-code="dog := Dog{}
cat := Cat{}

MakeItSpeak(dog) // Output: Woof!
MakeItSpeak(cat) // Output: Meow!
" style="color:#babed8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki material-theme-ocean" style="background-color: #0F111A" tabindex="0"><code><span class="line"><span style="color: #BABED8">dog </span><span style="color: #89DDFF">:=</span><span style="color: #BABED8"> Dog</span><span style="color: #89DDFF">{}</span></span>
<span class="line"><span style="color: #BABED8">cat </span><span style="color: #89DDFF">:=</span><span style="color: #BABED8"> Cat</span><span style="color: #89DDFF">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #82AAFF">MakeItSpeak</span><span style="color: #89DDFF">(</span><span style="color: #BABED8">dog</span><span style="color: #89DDFF">)</span><span style="color: #BABED8"> </span><span style="color: #464B5D; font-style: italic">// Output: Woof!</span></span>
<span class="line"><span style="color: #82AAFF">MakeItSpeak</span><span style="color: #89DDFF">(</span><span style="color: #BABED8">cat</span><span style="color: #89DDFF">)</span><span style="color: #BABED8"> </span><span style="color: #464B5D; font-style: italic">// Output: Meow!</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p><strong>3. Interface Practical Use Cases</strong> Interfaces in GoLang are widely used to make programs flexible and adaptable to different types without changing the code. This is especially useful for writing generic functions, allowing you to write code that can work with different types.</p>



<p>Here are a few practical use cases of interfaces:</p>



<ul class="wp-block-list">
<li><strong>Polymorphism</strong>: Interfaces allow different types to be treated uniformly, so you can create functions that accept interface types and work with any data type that implements the required methods.</li>



<li><strong>Dependency Injection</strong>: Interfaces are often used to inject dependencies, making testing and decoupling of components easier.</li>
</ul>



<p><strong>4. Empty Interfaces</strong> An empty interface is declared using <code>interface{}</code> and can hold values of any type. This is because every type implements at least zero methods, making them compatible with the empty interface.</p>



<p>Example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#181c2a;color:#aaafcf">Go</span><span role="button" tabindex="0" data-code="func Describe(i interface{}) {
    fmt.Printf(&quot;(%v, %T)\n&quot;, i, i)
}

Describe(42)       // Output: (42, int)
Describe(&quot;hello&quot;)   // Output: (hello, string)
" style="color:#babed8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki material-theme-ocean" style="background-color: #0F111A" tabindex="0"><code><span class="line"><span style="color: #89DDFF">func</span><span style="color: #BABED8"> </span><span style="color: #82AAFF">Describe</span><span style="color: #89DDFF">(</span><span style="color: #BABED8">i </span><span style="color: #89DDFF">interface{})</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    fmt</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Printf</span><span style="color: #89DDFF">(</span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">(</span><span style="color: #F07178">%v</span><span style="color: #C3E88D">, </span><span style="color: #F07178">%T</span><span style="color: #C3E88D">)</span><span style="color: #BABED8">\n</span><span style="color: #89DDFF">&quot;</span><span style="color: #89DDFF">,</span><span style="color: #BABED8"> i</span><span style="color: #89DDFF">,</span><span style="color: #BABED8"> i</span><span style="color: #89DDFF">)</span></span>
<span class="line"><span style="color: #89DDFF">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #82AAFF">Describe</span><span style="color: #89DDFF">(</span><span style="color: #F78C6C">42</span><span style="color: #89DDFF">)</span><span style="color: #BABED8">       </span><span style="color: #464B5D; font-style: italic">// Output: (42, int)</span></span>
<span class="line"><span style="color: #82AAFF">Describe</span><span style="color: #89DDFF">(</span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">hello</span><span style="color: #89DDFF">&quot;</span><span style="color: #89DDFF">)</span><span style="color: #BABED8">   </span><span style="color: #464B5D; font-style: italic">// Output: (hello, string)</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p>While the empty interface is powerful and flexible, it’s generally better to use more specific interfaces to ensure type safety and readability.</p>



<p><strong>5. Type Assertions and Type Switches</strong> When you work with an interface, you may need to retrieve the actual value or type. This is done using <strong>type assertions</strong> or <strong>type switches</strong>.</p>



<p>Here’s how to perform a type assertion:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#181c2a;color:#aaafcf">Go</span><span role="button" tabindex="0" data-code="var i interface{} = &quot;hello&quot;

s := i.(string) // Type assertion
fmt.Println(s)  // Output: hello
" style="color:#babed8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki material-theme-ocean" style="background-color: #0F111A" tabindex="0"><code><span class="line"><span style="color: #89DDFF">var</span><span style="color: #BABED8"> i </span><span style="color: #89DDFF">interface{}</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">=</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">hello</span><span style="color: #89DDFF">&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #BABED8">s </span><span style="color: #89DDFF">:=</span><span style="color: #BABED8"> i</span><span style="color: #89DDFF">.(</span><span style="color: #C792EA">string</span><span style="color: #89DDFF">)</span><span style="color: #BABED8"> </span><span style="color: #464B5D; font-style: italic">// Type assertion</span></span>
<span class="line"><span style="color: #BABED8">fmt</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Println</span><span style="color: #89DDFF">(</span><span style="color: #BABED8">s</span><span style="color: #89DDFF">)</span><span style="color: #BABED8">  </span><span style="color: #464B5D; font-style: italic">// Output: hello</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p>To handle cases where the assertion may fail:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#181c2a;color:#aaafcf">Go</span><span role="button" tabindex="0" data-code="s, ok := i.(string)
if ok {
    fmt.Println(s)  // Output: hello
} else {
    fmt.Println(&quot;Not a string&quot;)
}
" style="color:#babed8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki material-theme-ocean" style="background-color: #0F111A" tabindex="0"><code><span class="line"><span style="color: #BABED8">s</span><span style="color: #89DDFF">,</span><span style="color: #BABED8"> ok </span><span style="color: #89DDFF">:=</span><span style="color: #BABED8"> i</span><span style="color: #89DDFF">.(</span><span style="color: #C792EA">string</span><span style="color: #89DDFF">)</span></span>
<span class="line"><span style="color: #89DDFF; font-style: italic">if</span><span style="color: #BABED8"> ok </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    fmt</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Println</span><span style="color: #89DDFF">(</span><span style="color: #BABED8">s</span><span style="color: #89DDFF">)</span><span style="color: #BABED8">  </span><span style="color: #464B5D; font-style: italic">// Output: hello</span></span>
<span class="line"><span style="color: #89DDFF">}</span><span style="color: #BABED8"> </span><span style="color: #89DDFF; font-style: italic">else</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    fmt</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Println</span><span style="color: #89DDFF">(</span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">Not a string</span><span style="color: #89DDFF">&quot;</span><span style="color: #89DDFF">)</span></span>
<span class="line"><span style="color: #89DDFF">}</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p><strong>Type switches</strong> allow you to handle multiple types in a single statement:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#181c2a;color:#aaafcf">Go</span><span role="button" tabindex="0" data-code="func CheckType(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Printf(&quot;Integer: %d\n&quot;, v)
    case string:
        fmt.Printf(&quot;String: %s\n&quot;, v)
    default:
        fmt.Printf(&quot;Unknown type\n&quot;)
    }
}
" style="color:#babed8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki material-theme-ocean" style="background-color: #0F111A" tabindex="0"><code><span class="line"><span style="color: #89DDFF">func</span><span style="color: #BABED8"> </span><span style="color: #82AAFF">CheckType</span><span style="color: #89DDFF">(</span><span style="color: #BABED8">i </span><span style="color: #89DDFF">interface{})</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    </span><span style="color: #89DDFF; font-style: italic">switch</span><span style="color: #BABED8"> v </span><span style="color: #89DDFF">:=</span><span style="color: #BABED8"> i</span><span style="color: #89DDFF">.(</span><span style="color: #BABED8">type</span><span style="color: #89DDFF">)</span><span style="color: #BABED8"> </span><span style="color: #89DDFF">{</span></span>
<span class="line"><span style="color: #BABED8">    </span><span style="color: #89DDFF; font-style: italic">case</span><span style="color: #BABED8"> </span><span style="color: #C792EA">int</span><span style="color: #89DDFF">:</span></span>
<span class="line"><span style="color: #BABED8">        fmt</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Printf</span><span style="color: #89DDFF">(</span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">Integer: </span><span style="color: #F07178">%d</span><span style="color: #BABED8">\n</span><span style="color: #89DDFF">&quot;</span><span style="color: #89DDFF">,</span><span style="color: #BABED8"> v</span><span style="color: #89DDFF">)</span></span>
<span class="line"><span style="color: #BABED8">    </span><span style="color: #89DDFF; font-style: italic">case</span><span style="color: #BABED8"> </span><span style="color: #C792EA">string</span><span style="color: #89DDFF">:</span></span>
<span class="line"><span style="color: #BABED8">        fmt</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Printf</span><span style="color: #89DDFF">(</span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">String: </span><span style="color: #F07178">%s</span><span style="color: #BABED8">\n</span><span style="color: #89DDFF">&quot;</span><span style="color: #89DDFF">,</span><span style="color: #BABED8"> v</span><span style="color: #89DDFF">)</span></span>
<span class="line"><span style="color: #BABED8">    </span><span style="color: #89DDFF; font-style: italic">default</span><span style="color: #89DDFF">:</span></span>
<span class="line"><span style="color: #BABED8">        fmt</span><span style="color: #89DDFF">.</span><span style="color: #82AAFF">Printf</span><span style="color: #89DDFF">(</span><span style="color: #89DDFF">&quot;</span><span style="color: #C3E88D">Unknown type</span><span style="color: #BABED8">\n</span><span style="color: #89DDFF">&quot;</span><span style="color: #89DDFF">)</span></span>
<span class="line"><span style="color: #BABED8">    </span><span style="color: #89DDFF">}</span></span>
<span class="line"><span style="color: #89DDFF">}</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p><strong>Lesson Summary:</strong></p>



<ul class="wp-block-list">
<li><strong>Interfaces</strong> define a set of methods that types can implement, enabling polymorphism and flexibility in GoLang.</li>



<li>You can implement interfaces implicitly by defining methods that match the interface.</li>



<li><strong>Empty interfaces</strong> can hold values of any type but should be used cautiously to avoid losing type safety.</li>



<li>Use <strong>type assertions</strong> and <strong>type switches</strong> to extract concrete types from interfaces.</li>
</ul>



<p>This concludes <strong>Lesson 7: GoLang Interfaces</strong>. Understanding and utilizing interfaces in GoLang is key to writing reusable, flexible, and decoupled code.</p>
<p>The post <a href="https://irislogic.com/golang-interfaces/">GoLang Interfaces</a> appeared first on <a href="https://irislogic.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
