Skip to Content
Hands-On Software Engineering with Golang
book

Hands-On Software Engineering with Golang

by Achilleas Anagnostopoulos
January 2020
Intermediate to advanced
640 pages
16h 56m
English
Packt Publishing
Content preview from Hands-On Software Engineering with Golang

Looking up links

Looking up links is a fairly trivial operation. All we need to do is acquire a read lock, look up the link by its ID, and do either of the following things:

  • Return the link back to the caller
  • Return an error if no link with the provided ID was found

The link lookup logic is outlined in the following code snippet:

func (s *InMemoryGraph) FindLink(id uuid.UUID) (*graph.Link, error) {
    s.mu.RLock()
    defer s.mu.RUnlock()

    link := s.links[id]
    if link == nil {
        return nil, xerrors.Errorf("find link: %w", graph.ErrNotFound)
    }

    lCopy := new(graph.Link)
    *lCopy = *link
    return lCopy, nil
}

Since we want to ensure that no external code can modify the graph's contents without invoking the UpsertLink method, the FindLink implementation ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On Software Architecture with Golang

Hands-On Software Architecture with Golang

Jyotiswarup Raiturkar

Publisher Resources

ISBN: 9781838554491Supplemental Content