<?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>errors | www.valeriofinazzo.it</title>
	<atom:link href="http://www.valeriofinazzo.it/wordpress/tag/errors/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.valeriofinazzo.it/wordpress</link>
	<description>Il sito dell&#039;ingegnere Valerio Finazzo</description>
	<lastBuildDate>Fri, 30 Oct 2020 16:41:04 +0000</lastBuildDate>
	<language>it-IT</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.3</generator>
	<item>
		<title>TUTORIAL GO &#8211; GESTIONE DEGLI ERRORI</title>
		<link>http://www.valeriofinazzo.it/wordpress/2020/08/tutorial-go-gestione-degli-errori/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 12 Aug 2020 22:34:09 +0000</pubDate>
				<category><![CDATA[GOLANG]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[GO]]></category>
		<guid isPermaLink="false">http://www.valeriofinazzo.it/wordpress/?p=616</guid>

					<description><![CDATA[<p>Go introduce l&#8217;errore a livello di funzione, ovvero in Go solo una funzione può generare un errore e la sua presenza deve essere dichiarata tramite la parola chiave error Nell&#8217;esempio ho creato 3 funzioni con 3 comportamenti distinti: la prima non può generare un errore, la seconda può generare un errore, la terza restituisce una &#8230; <a href="http://www.valeriofinazzo.it/wordpress/2020/08/tutorial-go-gestione-degli-errori/" class="more-link">Continua la lettura di <span class="screen-reader-text">TUTORIAL GO &#8211; GESTIONE DEGLI ERRORI</span> <span class="meta-nav">&#8594;</span></a></p>
The post <a href="http://www.valeriofinazzo.it/wordpress/2020/08/tutorial-go-gestione-degli-errori/">TUTORIAL GO – GESTIONE DEGLI ERRORI</a> first appeared on <a href="http://www.valeriofinazzo.it/wordpress">www.valeriofinazzo.it</a>.]]></description>
										<content:encoded><![CDATA[<p>Go introduce l&#8217;errore a livello di funzione, ovvero in Go solo una funzione può generare un errore e la sua presenza deve essere dichiarata tramite la parola chiave error</p>



<pre class="crayon-plain-tag">package main

import (
	&quot;errors&quot;
	&quot;fmt&quot;
)

func noerrore(valore string) string {
	return valore
}

func possibileerrore(valore string) error {
	var error error
	if valore == &quot;prova&quot; {
		error = errors.New(&quot;Valore non ammesso&quot;)
	}
	return error
}

func valoreconpossibileerrore(valore string) (string, error) {
	var error error
	if valore == &quot;prova&quot; {
		error = errors.New(&quot;Valore non ammesso&quot;)
	}
	return valore, error
}

func main() {
	fmt.Println(noerrore(&quot;prova&quot;))
	fmt.Println(possibileerrore(&quot;prova&quot;))
	fmt.Println(valoreconpossibileerrore(&quot;prova&quot;))
}</pre>



<p>Nell&#8217;esempio ho creato 3 funzioni con 3 comportamenti distinti: la prima non può generare un errore, la seconda può generare un errore, la terza restituisce una stringa e può generare un errore. Per convenzione l&#8217;errore è l&#8217;ultimo tipo di valore restituito e in assenza di errore esso vale nil. Per generare l&#8217;errore ci viene in soccorso il package errors che mette a disposizione la funzione New che genera un errore contenente un testo.</p>



<p>Go ci permette di definire nuovi tipi di errori basati sul comando structs, la cosa fondamentale è che essi implementino l&#8217;interafaccia error che presenta il metodo Error da implementare</p>



<pre class="crayon-plain-tag">type myError struct {
	codice      int
	descrizione string
}

func (e *myError) Error() string {
	return fmt.Sprintf(&quot;%d - %s&quot;, e.codice, e.descrizione)
}

func possibileMyerrore(valore string) error {
	var error error
	if valore == &quot;prova&quot; {
		error = &amp;amp;myError{1, &quot;KO&quot;}
	}
	return error
}</pre>



<p>In questo caso ho definito un error custom che presenta i campi codice e descrizione. Ho implementato  la funzione Error() che sostanzialmente fa il toString dell&#8217;errore e implementato un metodo di esempio che lo utilizza</p>



<p>A questo punto il metodo restituisce una interfaccia error e pertanto non sono accessibili i campi della struct definita. Per ovviare a questa limite occorre sfruttare il meccanismo della type assertion che permette di forzare il tipo che si desidera (il meccanismo ricordo il casting di java) e il gioco è fatto.</p>



<pre class="crayon-plain-tag">var errore = possibileMyerrore(&quot;prova&quot;)
	var er = errore.(*myError)
	fmt.Println(er.codice)</pre>
<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.valeriofinazzo.it%2Fwordpress%2F2020%2F08%2Ftutorial-go-gestione-degli-errori%2F&amp;linkname=TUTORIAL%20GO%20%E2%80%93%20GESTIONE%20DEGLI%20ERRORI" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.valeriofinazzo.it%2Fwordpress%2F2020%2F08%2Ftutorial-go-gestione-degli-errori%2F&amp;linkname=TUTORIAL%20GO%20%E2%80%93%20GESTIONE%20DEGLI%20ERRORI" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwww.valeriofinazzo.it%2Fwordpress%2F2020%2F08%2Ftutorial-go-gestione-degli-errori%2F&amp;linkname=TUTORIAL%20GO%20%E2%80%93%20GESTIONE%20DEGLI%20ERRORI" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=http%3A%2F%2Fwww.valeriofinazzo.it%2Fwordpress%2F2020%2F08%2Ftutorial-go-gestione-degli-errori%2F&amp;linkname=TUTORIAL%20GO%20%E2%80%93%20GESTIONE%20DEGLI%20ERRORI" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_google_gmail" href="https://www.addtoany.com/add_to/google_gmail?linkurl=http%3A%2F%2Fwww.valeriofinazzo.it%2Fwordpress%2F2020%2F08%2Ftutorial-go-gestione-degli-errori%2F&amp;linkname=TUTORIAL%20GO%20%E2%80%93%20GESTIONE%20DEGLI%20ERRORI" title="Gmail" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=http%3A%2F%2Fwww.valeriofinazzo.it%2Fwordpress%2F2020%2F08%2Ftutorial-go-gestione-degli-errori%2F&#038;title=TUTORIAL%20GO%20%E2%80%93%20GESTIONE%20DEGLI%20ERRORI" data-a2a-url="http://www.valeriofinazzo.it/wordpress/2020/08/tutorial-go-gestione-degli-errori/" data-a2a-title="TUTORIAL GO – GESTIONE DEGLI ERRORI"></a></p>The post <a href="http://www.valeriofinazzo.it/wordpress/2020/08/tutorial-go-gestione-degli-errori/">TUTORIAL GO – GESTIONE DEGLI ERRORI</a> first appeared on <a href="http://www.valeriofinazzo.it/wordpress">www.valeriofinazzo.it</a>.]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
