yossiytd
(yossi)
June 4, 2018, 10:00pm
1
hey guys
im new at go and i will be happy to simple help to understand the lang.
i have items as i scraped , simple things like pics , prices and title of item now i can print each value , but how i can store that values on array ?
finally i want to get array of items
e.DOM.Find(".sresult").Each(func(index int, item *goquery.Selection) {
items := item
price := items.Find(".prc .bold").Text()
title := items.Find(".vip").Text()
pic, _ := items.Find(".imgWr img").Attr("imgurl")
fmt.Println(index, title, price, pic)
}
theckman
(Tim Heckman)
June 13, 2018, 3:23pm
3
The first thing I’ll say is that you probably don’t want an array, and probably want a slice instead. In Go they are similar, but have some major differences.
Looking through your post it’s hard to provide specific guidance because we don’t have enough context to fully understand the code. I also have never used goquery
so I’m not entirely sure how everything works. It looks like you have printing of the items figured out, next you just need to add them to a slice.
The Go tour does a good job at explaining Arrays and Slices, and I would suggest going through that if you’ve not already:
You can use a slice of a struct. For example
type MyItem {
Index int
Title string
price float64
Pic string
}
var items []MyItems
// Create item
item := MyItem{ 1, “Item Title”, 100.25, “MyPic.png”}
// Add to slice
items = append(items, item)
HTH
Yamil
petkop
(Petko Petkov)
June 13, 2018, 3:42pm
5
This is a great read on how to start with slices in go https://blog.golang.org/go-slices-usage-and-internals
yossiytd
(yossi)
June 14, 2018, 12:06pm
6
thnx guys
i am understanding teh basic of data structchers ,my only problem is to work with dynamic data.
i will explain with Yamil_Bracho example
package main
import (
“fmt”
)
type MyItem struct {
Index int
Title string
price float64
Pic string
}
var items []MyItem
func main() {
item := MyItem{1, “Item Title”, 100.25, “MyPic.png”}
item = MyItem{2, “new Title”, 150.25, “MyPic.png”}
// Add to slice
items = append(items, item)
fmt.Println(items)
}
// Create item
final result is just the last item added to the slice , and i want to create list of items and convert them to json together
NobbZ
(Norbert Melzer)
June 14, 2018, 12:19pm
7
yossiytd:
func main() {
item := MyItem{1, “Item Title”, 100.25, “MyPic.png”}
item = MyItem{2, “new Title”, 150.25, “MyPic.png”}
// Add to slice
items = append(items, item)
fmt.Println(items)
}
You are creating a first item, then overwrite it instantly, and then only append the last item to an empty slice.
func main() {
item := MyItem{1, “Item Title”, 100.25, “MyPic.png”}
items = append(items, item)
item = MyItem{2, “new Title”, 150.25, “MyPic.png”}
items = append(items, item)
fmt.Println(items)
}
Is probably more what you want.
petkop
(Petko Petkov)
June 14, 2018, 12:32pm
8
@yossiytd ,
If you want to convert your data into json output, then just marshal the data.
https://blog.golang.org/json-and-go
package main
import (
"encoding/json"
"fmt"
)
type Item struct {
Index int
Title string
Price float64
Pic string
}
func main() {
item := Item{1, "Item Title", 100.25, "MyPic.png"}
item = Item{2, "new Title", 150.25, "MyPic.png"}
b, err := json.Marshal(item)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("Result: %s\n",b)
}
Output
Result: {"Index":2,"Title":"new Title","Price":150.25,"Pic":"MyPic.png"}
Playground - https://play.golang.org/p/QBeFfFqbwyb
system
(system)
Closed
September 12, 2018, 12:32pm
9
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.