Home >Backend Development >Golang >How can I efficiently map tags to image URLs using pointers in Go while ensuring proper garbage collection?
Garbage Collection and Correct Usage of Pointers in Go
Problem:
You're working with a language with a different data structure, such as Python, and you want to migrate to Go. You're not sure how to leverage pointers properly in a Go program.
Specifically, you have a situation where:
Question:
How can you use pointers effectively to map tags to image URLs in a Go program? What considerations should be taken to ensure proper garbage collection and memory management?
Answer:
1. Avoid Storing Pointers to Fields:
2. String Values Behave Like Pointers:
3. Use a String Interner:
Example Code:
<code class="go">package main import ( "encoding/json" "fmt" "os" "reflect" ) type Image struct { URL string Description string Tags []*Tag } type Tag struct { Name string Rank int } // this function mimics json.NewDecoder(resp.Body).Decode(&parsedJSON) func searchImages() []*Image { parsedJSON := []*Image{ &Image{ URL: "https://c8.staticflickr.com/4/3707/11603200203_87810ddb43_o.jpg", Description: "Ocean islands", Tags: []*Tag{ &Tag{"ocean", 1}, &Tag{"water", 2}, &Tag{"blue", 3}, &Tag{"forest", 4}, }, }, &Image{ URL: "https://c3.staticflickr.com/1/48/164626048_edeca27ed7_o.jpg", Description: "Bridge over river", Tags: []*Tag{ &Tag{"bridge", 1}, &Tag{"river", 2}, &Tag{"water", 3}, &Tag{"forest", 4}, }, }, } return parsedJSON } func main() { result := searchImages() cache := map[string]string{} tagToUrlMap := make(map[string][]string) for _, image := range result { imageURL := intern(image.URL, cache) for _, tag := range image.Tags { tagName := intern(tag.Name, cache) tagToUrlMap[tagName] = append(tagToUrlMap[tagName], imageURL) } } cache = nil enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") if err := enc.Encode(tagToUrlMap); err != nil { panic(err) } } func intern(s string, cache map[string]string) string { if s2, ok := cache[s]; ok { return s2 } // New string, store it cache[s] = s return s }</code>
Additional Resources:
The above is the detailed content of How can I efficiently map tags to image URLs using pointers in Go while ensuring proper garbage collection?. For more information, please follow other related articles on the PHP Chinese website!