<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Roman Shirokov on Medium]]></title>
        <description><![CDATA[Stories by Roman Shirokov on Medium]]></description>
        <link>https://medium.com/@shirokovroma?source=rss-743ab2581748------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*jzR0Un9INnfVuQ6n.jpg</url>
            <title>Stories by Roman Shirokov on Medium</title>
            <link>https://medium.com/@shirokovroma?source=rss-743ab2581748------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 20:12:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shirokovroma/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Go Range over function example]]></title>
            <link>https://medium.com/@shirokovroma/go-range-over-function-example-02520e462bbd?source=rss-743ab2581748------2</link>
            <guid isPermaLink="false">https://medium.com/p/02520e462bbd</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[iterators]]></category>
            <category><![CDATA[range]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <dc:creator><![CDATA[Roman Shirokov]]></dc:creator>
            <pubDate>Mon, 19 Feb 2024 22:59:26 GMT</pubDate>
            <atom:updated>2024-02-20T16:50:33.456Z</atom:updated>
            <content:encoded><![CDATA[<p>In the new Go language version 1.22.0, the ‘function iterators’ experiment was introduced. Let’s try a simple example of the new functionality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VciUbRWy3hg0NYAjYuKAGQ.png" /></figure><h3>Setup</h3><p>Install go version 1.22.0<br>If you use vscode, add the env to .vscode/launch.json file:</p><pre>{<br>    &quot;version&quot;: &quot;0.2.0&quot;,<br>    &quot;configurations&quot;: [<br>        {<br>            &quot;name&quot;: &quot;Launch Package&quot;,<br>            &quot;type&quot;: &quot;go&quot;,<br>            &quot;request&quot;: &quot;launch&quot;,<br>            &quot;mode&quot;: &quot;auto&quot;,<br>            &quot;program&quot;: &quot;${fileDirname}&quot;,<br>            &quot;env&quot;: {<br>                &quot;GOEXPERIMENT&quot;: &quot;rangefunc&quot;<br>            }<br>        }<br>    ]<br>}</pre><h3>Iterator for slice of strings example</h3><p>Create main.go file, init module with go mod init sampleiter</p><p>First import new experimental iter package.</p><pre>package main<br><br>import (<br> &quot;fmt&quot;<br> &quot;iter&quot; // Assuming &#39;iter&#39; is the experimental package for the iterator proposal<br>)</pre><p>Define a function IterateStrings that takes a slice of strings and returns an iterator (iter.Seq[string]) for iterating over the slice.</p><pre>func IterateStrings(slice []string) iter.Seq[string] </pre><p>Inside the function, it returns an anonymous function (which matches the Seq type definition) that takes a yield function as a parameter.</p><pre>return func(yield func(string) bool) </pre><p>The for loop iterates over each string in the slice, calls the yield function with the string. If the yield function returns false, it breaks the loop, allowing termination in the end of sequence.</p><pre>for _, str := range slice { <br> if !yield(str) { // Yield each string until false<br>  break<br> }<br>}</pre><p>In the main function define slice with data and for loop over IterateStrings .</p><pre>func main() {<br> // Example slice of strings<br> myStrings := []string{&quot;Go&quot;, &quot;Rangefunc&quot;, &quot;Experiment&quot;}<br><br> // Using the iterator with the range-over-function syntax<br> for str := range IterateStrings(myStrings) {<br>  fmt.Println(str) // Prints each string in the slice<br> }<br>}</pre><p>Full example:</p><pre>package main<br><br>import (<br> &quot;fmt&quot;<br> &quot;iter&quot; // Assuming &#39;iter&#39; is the experimental package for the iterator proposal<br>)<br><br>func IterateStrings(slice []string) iter.Seq[string] {<br> return func(yield func(string) bool) {<br>  for _, str := range slice { <br>   if !yield(str) {<br>    break<br>   }<br>  }<br> }<br>}<br><br>func main() {<br> myStrings := []string{&quot;Go&quot;, &quot;Rangefunc&quot;, &quot;Experiment&quot;}<br><br> // Using the iterator with the range-over-function syntax<br> for str := range IterateStrings(myStrings) {<br>  fmt.Println(str) <br> }<br>}</pre><p>To run it: GOEXPERIMENT=rangefunc go run main.go or run with vscode</p><p>Output:</p><pre>Go<br>Rangefunc<br>Experiment</pre><h3>Links</h3><p>Implementation: <a href="https://go.googlesource.com/go/+/refs/changes/41/510541/7/src/cmd/compile/internal/rangefunc/rewrite.go">https://go.googlesource.com/go/+/refs/changes/41/510541/7/src/cmd/compile/internal/rangefunc/rewrite.go</a><br>Wiki description: <a href="https://go.dev/wiki/RangefuncExperiment">https://go.dev/wiki/RangefuncExperiment</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=02520e462bbd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>