Mailgun syntax error: non-declaration statement outside function body

hello

I get this error when tring to build the code below:

syntax error: non-declaration statement outside function body

I’m using windows 7, the newest go build, downloaded today.

I managed to install the mailgun library from https://github.com/mailgun/mailgun-go
and the docs for mailgun is here documentation.mailgun.com/quickstart-sending.html#send-with-smtp-or-api

And trying to use the mailgun library,

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?

package main

import “gopkg.in/mailgun/mailgun-go.v1

//mg := mailgun.NewMailgun(“sandbox33033.mailgun.org”, “key-477np2zwszktfmqzy2sei2rtoatb0xq6”, “pubkey-5cchzfbkay2ppp8nc99ynanqa56-a797”)
mg := mailgun.NewMailgunFromEnv()
message := mailgun.NewMessage(
"sender@example.com",
“Fancy subject!”,
“Hello from Mailgun Go!”,
"recipient@example.com")
resp, id, err := mg.Send(message)
if err != nil {
log.Fatal(err)
}

func main() {
fmt.Printf(“hello, world\n”)
fmt.Printf(“ID: %s Resp: %s\n”, id, resp)
}

Thank you!

The message := ... short declaration syntax is only allowed inside functions or methods. You want var message = ... instead.

(You also want [code]...[/code] tags around the code ;))

Jackob, Thank you!

this is the new code, after your sugg., but still getting the same error:

c.go:15: syntax error: non-declaration statement outside function body

package main

import "gopkg.in/mailgun/mailgun-go.v1"

var mg = mailgun.NewMailgunFromEnv()
var message = mailgun.NewMessage(
    "sender@example.com",
    "Fancy subject!",
    "Hello from Mailgun Go!",
    "recipient@example.com")
var resp, id, err = mg.Send(message)
if err != nil { ------------------------------> this is line 15
    log.Fatal(err)
}


func main() {
    fmt.Printf("hello, world\n")
    fmt.Printf("ID: %s Resp: %s\n", id, resp)
}

I’m sure that you’ll find the problem :slight_smile:

Thank you!

I think I see what is happening. The example code, https://github.com/mailgun/mailgun-go#sending-mail-via-the-golang-library, is not a valid go program. I filed an issue.

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:

package main

import (
	"fmt"
	"log"

	"gopkg.in/mailgun/mailgun-go.v1"
)

func main() {
	log.SetFlags(log.Lshortfile)

	mg, err := mailgun.NewMailgunFromEnv()
	if err != nil {
		log.Fatal(err)
	}

	message := mailgun.NewMessage(
		"sender@example.com",
		"Fancy subject!",
		"Hello from Mailgun Go!",
		"recipient@example.com")

	resp, id, err := mg.Send(message)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("hello, world\n")
	fmt.Printf("ID: %s Resp: %s\n", id, resp)
}

I updated the code so it compiles and so all errors are checked using my error checking while prototyping method method.

3 Likes

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.

Thank you Nathan !

1 Like

Nathan, it works!

but only after i deleted the “err” from declaration, because I received this error at runtime:

why? it works for you and for me it gives this error?

assignment count mismatch 2 = 1

mg, err := mailgun.NewMailgunFromEnv()
	if err != nil {
		log.Fatal(err)
	}

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

This is the latest version on the 1.1.0 branch

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.

What version and import are you using?

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

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.

mg := mailgun.NewMailgun("mg.prxxxxxxxxc.eu", "key-xxxxxxxxxxxxx", "pubkey-5ccxxxxxxxxxxx")

so the code right now is like this:

    .....
        func main() {
        	log.SetFlags(log.Lshortfile)

        	//mg, err := mailgun.NewMailgunFromEnv()
            //mg, err := mailgun.NewMailgun()
        	mg := mailgun.NewMailgun("mg.pxxxxx.eu", "key-xxxxxxxxxx0xq6", "pxxxxxxx56-a797")
        	
        	// if err != nil {
        	// 	log.Fatal(err)
        	// }

        	message := mailgun.NewMessage(
        .............

Hmm… That’s the same commit…

Can you try copy/pasting my example into a new file and compiling just it?

this is the code I build:

package main
// si e doar pt build

import (
	"fmt"
	"log"

	"gopkg.in/mailgun/mailgun-go.v1"
)

func main() {
	log.SetFlags(log.Lshortfile)

	//mg, err := mailgun.NewMailgunFromEnv()
	mg, err := mailgun.NewMailgun("mg.xxxxxxx.eu", "key-477xxxxxxxxq6", "pubkey-5xxanqa56-a797")
	
	if err != nil {
		log.Fatal(err)
	}

	message := mailgun.NewMessage(
		"PRxxxx SRL <noreply@xxxxxxx.eu>",
		"Atasat aveti factura",
		"body Hello from Mailgun Go!",
		"gxxxxxxxxx1+testmailgun@gmail.com")

	resp, id, err := mg.Send(message)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("hello, world\n")
	fmt.Printf("ID: %s Resp: %s\n", id, resp)
}

The difference is you are calling mailgun.NewMailgun instead of mailgun.NewMailgunFromEnv. The methods have different signatures.

yes this is why I deleted the err, to bypass the error, and now is working :slight_smile:

NewMailgun doesn’t return an error. That’s why the compiler complained.

for me i get the error go:18: assignment count mismatch: 2 = 1

mg, err := mailgun.NewMailgun("mg.xxxxxxx.eu", "key-477xxxxxxxxq6", "pubkey-5xxanqa56-a797")

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. :slight_smile:
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
:slight_smile:
anyway Thank you! Nathan :slight_smile:

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.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.