I copied the following code from the mailgun documentation, and I get this error, I’m new to Go language, and to programming in general, I know to code in html, and a little php, but is hard to me to understand the concepts of namespaces, functions, and classes, I know that is related to something like this, becasue when I wanted to post the new topic I got this sugestion X := "somevalue" works in the main func, but not outside
can someone help me readjust the code so that, I can make a working script from this one?
Most code you write will be inside a function. Only declarations (e.g., package, import, var, const, type, func, or label) are allowed outside of functions. The solution to the errors you received is to move the code into a function:
I’ll give it a try, and I’ll give feedback, anyway, for a beginner if the programmer that made the library gives you the wrong sample, is very hard to know that the sample code is not OK.
My initial guess is that we are using different versions of mailgun.
Running git log -n 1 in $GOPATH/src/gopkg.in/mailgun/mailgun-go.v1 gives me:
commit a4002e2df2e8ca2da6a6fbb4a72871b504e49f50
Merge: 1dbc18b f4918ee
Author: Derrick J. Wippler <thrawn01@gmail.com>
Date: Mon Feb 13 09:09:30 2017 -0600
Merge pull request #91 from c2h5oh/master
Always runs tests on latest patch release of each minor Go version
Git blame says the function declaration for NewMailgunFromEnv was last changed in 2ababce. It did not exist in the code base in the parent commit to that one.
but I had to use other function instead, because the NewmailgunFromEnv() it gives me an error that is not finding a required variable, altough I’ve already set it.
tries to assign the output from mailgun.NewMailgun to mg, and err. The left-hand-side (mg, err) has two items. malign.NewMailgun only returns one item. The number of items returned (1) does not match (is not the same as) the number of items that are being assigned (2).
I thought that err is just for catching errors, and Is used in a good way by the author of the code.
As you probably know I just take the code from others and adapt what I cand understand like domain name, api key , user etc, and sometimes, a do litle adjustments like deleting what I think is unecesary like err in this case
anyway Thank you! Nathan
The only support go has for error handling is the builtin error type. The result is that errors are handled like every other type. Since go functions can return multiple values, there is a convention that when a function returns an error it returns it as the last value.
There is no throwing/catching of errors. Errors are values. This means you have access to the full capabilities of go to deal with them.