Structs in maps

Hi all i am trying have a struct in map but cant get it to work
any help would appreciated

type Post struct {
	ID      int
	Content string
	Created time.Time
	Updated time.Time
}

type Posts struct {
	posts map[int]*Post
}

func (p Posts) PNum() int {
	return len(p.posts) + 1
}

func NewPost(p Posts, pp Post) *Posts {
	n := p.PNum()
	return &Posts{
		posts[n] = &Post{
			ID:      pp.ID,
			Content: pp.Content,
			Created: time.Now(),
		},
	}
}

The following returns one of your structs with a map literal in it, which I think is what you’re trying to do based on your code, the part that does not compile. If it’s not, please explain what is you are trying to do.

func NewPost(p Posts, pp Post) *Posts {
	n := p.PNum()
	return &Posts{
		posts: map[int]*Post{
			n: &Post{
				ID:      pp.ID,
				Content: pp.Content,
				Created: time.Now(),
			},
		},
	}
}

I suggest you change PNum function to be a counter instead of map length, if you remove a Post from the map lower down you could end up overwriting map elements higher up.

If post ID is unique, you could use that as your map key. Or even better, you could use slices depending on your use case and simply append the new post(s).

Here is a rough example: https://play.golang.org/p/uMUeOY-rn9t

thank you for the replies @calmh @norrchr learnt alot from both replies, this was a little project to learn interfaces and dependency injection

 package main

import (
	"fmt"
	"time"
)

type Service struct {
	MyPo MyPost
	MyUs MyUser
}

type MyUser interface {
	DeleteUser() error
	EditUser()
	ShowUser() error
}

type MyPost interface {
	DeletePost() error
	ShowPost() error
}

type User struct {
	ID       int
	Name     string
	Username string
	Password string
}

type Post struct {
	ID      int
	Content string
	Created time.Time
	Updated time.Time
}

type Posts struct {
	posts map[int]*Post
}

// func (p Posts) PNum() int {
// 	return len(p.posts) + 1
// }

//! user methods

// func NewPost(p Posts, pp Post) *Posts {
// 	n := p.PNum()
// 	return &Posts{
// 		posts[n] = &Post{
// 			ID:      pp.ID,
// 			Content: pp.Content,
// 			Created: time.Now(),
// 		},
// 	}
// }

func NewPost(p Post) *Post {
	return &Post{
		ID:      p.ID,
		Content: p.Content,
		Created: time.Now(),
	}

}

func (p Post) DeletePost() error {
	fmt.Printf("Deleted Post %v\n", p.ID)
	return nil
}

func (p Post) ShowPost() error {
	fmt.Printf("Show Post %v\n", p)
	return nil
}

func (p Post) EditPost() error {
	fmt.Printf("Edited Post %v\n", p.ID)
	return nil
}

func NewUser(u User) *User {
	return &User{
		ID:       u.ID,
		Name:     u.Name,
		Username: u.Username,
		Password: u.Password,
	}

}

//! user methods

func (u User) DeleteUser() error {
	fmt.Printf("deleted User %v\n", u.Name)
	return nil
}

func (u User) ShowUser() error {
	fmt.Printf("Show User %v\n", u)
	return nil
}

func (u User) EditUser() {
	fmt.Printf("%v Edited User \n", u.Name)
}

//! service new and methods

func New(post MyPost, user MyUser) *Service {

	s := &Service{
		MyPo: post,
		MyUs: user,
	}

	return s

}

func (s Service) DeleteUser(user *User) error {
	return user.DeleteUser()
}

func (s Service) EditUser(user *User) {
	user.EditUser()
}

func (s Service) ShowUser(user *User) {
	user.ShowUser()
}

func (s Service) DeletePost(post *Post) {
	post.DeletePost()
}

func (s Service) ShowPost(post *Post) error {
	return post.ShowPost()
}

func (s Service) EditPost(post *Post) {
	post.EditPost()
}

func main() {
	user1 := &User{2, "cg", "ccaglak", "secret"}
	post1 := &Post{ID: 1, Content: "yahoo", Created: time.Now()}

	u := NewUser(*user1)
	p := NewPost(*post1)

	service := New(p, u)

	service.MyPo.DeletePost()
	service.MyPo.ShowPost()
	service.MyUs.EditUser()
	service.MyUs.DeleteUser()

}

You have a lot of (helper?) functions which seems to allocate another structure for return. Your NewPost function takes Post, which then creates a new structure and returns its pointer address.

func NewPost(p Post) *Post {
 return &p
}

This is basically what you are doing, but you are basically copying the original Post into a new *Post structure.

I assume your wanting something more along the lines of below?

func NewPost(id int, content string) *Post {
 return &Post{ID: id, Content: content, Created: time.Now()}
}

@norrchr my intention was to initialize with NewPost and create method which will add will posts


// main.go
package main

import (
	"time"
)

type Service struct {
	MyPost
	MyUser
}

func NewService(u MyUser, p MyPost) *Service {
	return &Service{
		MyUser: u,
		MyPost: p,
	}

}

func (s Service) DeleteUser(n int) error {
	s.MyUser.DeleteUser(n)
	return nil
}

func (s Service) EditUser(n int) {
	s.MyUser.EditUser(n)
}

func (s Service) ShowUser(n int) {
	s.MyUser.ShowUser(n)
}

func (s Service) CreatePost(n int, p *Post) {

	s.MyPost.CreatePost(n, p)
}

func (s Service) DeletePost(n int) {
	s.MyPost.DeletePost(n)

}

func (s Service) ShowPost(n int) {
	s.MyPost.ShowPost(n)
}

func (s Service) EditPost(n int, p *Post) {

	s.MyPost.EditPost(n, p)
}

func main() {
	user1 := &User{2, "chad caglak ", "ecaglak", "chad secret"}
	post1 := &Post{ID: 1, Content: "yahoo", Created: time.Now()}
	post2 := &Post{ID: 2, Content: "google", Created: time.Now()}
	post3 := &Post{ID: 3, Content: "bing", Created: time.Now()}
	post4 := &Post{ID: 4, Content: "excite", Created: time.Now()}
	post5 := &Post{ID: 5, Content: "bing google", Created: time.Now()}
	post6 := &Post{ID: 6, Content: "excite altavista", Created: time.Now()}

	edit1 := &Post{ID: 7, Content: "edited  wow excite altavista", Created: time.Now(), Updated: time.Now()}
	edit2 := &Post{ID: 8, Content: "edit tata excite altavista", Created: time.Now(), Updated: time.Now()}

	u := NewUser(*user1)
	p := NewPost(1, *post1)

	s := NewService(u, p)
	s.CreatePost(2, post2)
	s.CreatePost(3, post3)
	s.CreatePost(4, post4)

	s.ShowAll()

	s.DeletePost(3)

	s.ShowAll()

	s.ShowPost(2)

	s.CreatePost(5, post5)
	s.CreatePost(6, post6)

	s.ShowAll()

	s.EditPost(2, edit1)
	s.EditPost(3, edit2)

	s.ShowAll()

}

// users.go not complete yet

type MyUser interface {
	CreateUser() *User
	DeleteUser(int) error
	EditUser(int) *User
	ShowUser(int)
}

type User struct {
	ID       int
	Name     string
	Username string
	Password string
}

func NewUser(u User) *User {
	return &User{
		ID:       u.ID,
		Name:     u.Name,
		Username: u.Username,
		Password: u.Password,
	}
}

func (u User) CreateUser() *User {
	fmt.Println("user Created")
	return &User{
		ID:       u.ID,
		Name:     u.Name,
		Username: u.Username,
		Password: u.Password,
	}
}

func (u User) DeleteUser(n int) error {
	fmt.Printf("User Delete %v", u.ID)
	return nil
}

func (u User) ShowUser(n int) {
	fmt.Printf("User Delete %v", u.ID)

}

func (u User) EditUser(n int) *User {
	return &User{
		ID:       u.ID,
		Name:     u.Name,
		Username: u.Username,
		Password: u.Password,
	}
}


// posts.go

type MyPost interface {
	CreatePost(int, *Post)
	DeletePost(int)
	EditPost(int, *Post)
	ShowPost(int)
	ShowAll()
}

type Post struct {
	ID      int
	Content string
	Created time.Time
	Updated time.Time
}
type Posts struct {
	post map[int]*Post
}

func NewPost(n int, p Post) *Posts {
	return &Posts{
		post: map[int]*Post{
			n: &Post{
				ID:      p.ID,
				Content: p.Content,
				Created: time.Now(),
			},
		},
	}
}

func (p Posts) PNum() int {
	return len(p.post) + 1
}

func (p Posts) CreatePost(n int, pp *Post) {
	p.post[n] = pp
	fmt.Println(" Post Created")
}

func (p Posts) DeletePost(n int) {
	delete(p.post, n)
	fmt.Println("Delete Post")
}

func (p Posts) EditPost(n int, pp *Post) {
	p.post[n] = pp
	fmt.Println(" Post Edited")
}

func (p Posts) ShowPost(n int) {
	fmt.Printf("Showing Post: %v\n", p.post[n])
}

func (p Posts) ShowAll() {

	for _, allp := range p.post {
		fmt.Println(allp)
	}
}

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