Syntax error: unexpected map at end of statement

Compiler gives syntax error: unexpected map at end of statement.
What’s I do wrong?

func (book Book) GetInfo() map[string]string {
	new_map := make(map[string]string);
	new_map["title"] = book.title;
	new_map["author"] = book.author;
	new_map["subject"] = book.subject;
	new_map["id"] = fmt.Sprintf("%d", book.id);
	new_map["price"] = fmt.Sprintf("%d", book.price);

	if (book.availible) {
		new_map["availible"] = "true";
	} else {
		new_map["availible"] = "false";
	}

	return new_map;
}

Can you please provide the line number of the error message and tell us which one from your snippet it points to?

I think the problem is “". This only can be used a first character so change new_map to newMap or something without "”…

You’re probably missing some closing bracket before the method definition. For example, if you remove main's closing bracket in this working example you’ll get the same error:

package main

import (
	"fmt"
)

type Book struct {
	title string
	author string
	subject string
	id int64
	price int64
	available bool
}


func main() {
	book := Book{
		title: "Title",
		author: "Author",
		subject: "Subject",
		id: 1,
		price: 99,
		available: true,
	}
	fmt.Printf("book: %+v\n", book.GetInfo())
}

func (book Book) GetInfo() map[string]string {
	new_map := make(map[string]string);
	new_map["title"] = book.title;
	new_map["author"] = book.author;
	new_map["subject"] = book.subject;
	new_map["id"] = fmt.Sprintf("%d", book.id);
	new_map["price"] = fmt.Sprintf("%d", book.price);

	if (book.available) {
		new_map["availible"] = "true";
	} else {
		new_map["availible"] = "false";
	}

	return new_map;
}

On a different note, it’s not idiomatic to include semicolons to terminate statements unless you have more than one in the same line, which isn’t idiomatic either.

1 Like

this one.

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