<p dir="auto">Currently, the stringer tool generates the method <code class="notranslate">func (t Type) String() string</code> to get the string name of an enum value.</p>
<p dir="auto">In several projects I have had the need of exactly the opposite: <strong>get the enum value from the string name</strong> (for example when parsing a flag or when reading a configuration file).<br>
I have ended up using a <code class="notranslate">switch</code> or a <code class="notranslate">map</code>, but I don't like it as I have to manually maintain it when values are added/changed/deleted. So the next step is to auto-generate it, just like the stringer tool does.</p>
<p dir="auto">I was wondering if adding this functionality to <code class="notranslate">stringer</code> would be adequate for you guys.  In my opinion, it seems pretty convenient and fits very well in that tool. I am sure there will be other people in the same situation than me that will benefit from it.</p>
<p dir="auto">A possible signature for the generated function could be:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="func &lt;Type&gt;FromString(s string) (&lt;Type&gt;, error) {...}

// Or
func &lt;Type&gt;String(s string) (&lt;Type&gt;, error) {...}"><pre class="notranslate"><code class="notranslate">func &lt;Type&gt;FromString(s string) (&lt;Type&gt;, error) {...}

// Or
func &lt;Type&gt;String(s string) (&lt;Type&gt;, error) {...}
</code></pre></div>
<p dir="auto">For example, if the type is <code class="notranslate">Pill</code> the function would be</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="func PillFromString(s string) (Pill, error) {...}

// Or
func PillString(s string) (Pill, error) {...}"><pre class="notranslate"><code class="notranslate">func PillFromString(s string) (Pill, error) {...}

// Or
func PillString(s string) (Pill, error) {...}
</code></pre></div>
<p dir="auto">The implementation could leverage the same variable the stringer tool already generates (a string or a map, depending on the number of runs found in the enum values). This requires a deeper reasoning (maybe a <code class="notranslate">switch</code> is the best option here)<br>
Having an <code class="notranslate">error</code> as a second return value is needed to notify about strings that do not belong to the enum type.</p>
<p dir="auto">The code to add is relatively simple and I can contribute with the needed changes.<br>
Before doing that, I would like to hear your opinions/suggestions and know if you finally agree.</p>
<p dir="auto">by <strong>cooldudevamsee</strong>:</p>
<pre class="notranslate">On ubuntu server, I have been trying to install go and was able to install go partially.
8l &amp; three more binaries are getting built in GOBIN but not 8g. Issue found to be
missing package bison. There is no meaningful error to report missing dependencies
during build process.


Thanks.</pre>