When the RSS feeds updates (it doesn't right now, just dummy data) the new items are appended to the "feed" slice. Over time this could mean that it contains millions of items, I don't want that.
So when there are more than 100 items in the slice it should delete items starting from the top (item 0). In this example I'm using an RSS file with ust 100 items so the sample code below should delete from the top after 50 items:
package main
import (
"fmt"
"github.com/SlyMarbo/rss"
"time"
)
var feed *rss.Feed
var directory = "./dump"
func main() {
for {
checkRSS()
// Check every minute if feed.Refresh has passed so it run's update()
time.Sleep(1 * time.Minute)
}
}
func checkRSS() (*rss.Feed, error) {
var err error
// If feed is still empty fetch it first so we can run update()
if feed == nil {
feed, err = rss.Fetch("http://cloud.dgier.nl/api.xml")
} else {
err = feed.Update()
}
length := len(feed.Items)
for key, value := range feed.Items {
fmt.Println(key, value.Title, value.Read)
if key >= 50 {
fmt.Println("Item key is > 50")
}
}
fmt.Printf("Current length: %d\n", length)
fmt.Printf("Refreshing at %s\n", feed.Refresh)
return feed, err
}
If the number of items in the feed grows over the limit, slice it:
length := len(feed.Items)
if length > limit {
feed.Items = feed.Items[length - limit:]
}
When the length is over the limit, the new length will be exactly limit.
You don't need a for loop there.
To achieve this you probably want to use subslicing. Say you want to remove x items from feed, you can simply do feed = feed[x:] which will yield all items after index x-1 and assign it back to the feed slice. If in your actual code you just want to remove the first item then it would be feed = feed[1:]
Related
with the following sample, I can add a new column that is diverted from the row values.
it's working well.
package main
import (
"fmt"
"strings"
"github.com/go-gota/gota/dataframe"
"github.com/go-gota/gota/series"
)
func main() {
csvStr := `accountId,deposit,Withdrawals
anil0001,50,10
vikas0002,10,10
ravi0003,20,10
user1111,NaN,20`
df := dataframe.ReadCSV(strings.NewReader(csvStr))
// Within a row, elements are indexed by their column index.
indexDeposit := 1
indexWithdrawals := 2
// Rapply reads the data by rows.
// You can access each element of the row using
// s.Elem(index) or s.Val(index).
// To browse by columns use Capply.
s := df.Rapply(func(s series.Series) series.Series {
deposit, err := s.Elem(indexDeposit).Int()
if err != nil {
return series.Ints("NAN")
}
withdrawal, err := s.Elem(indexWithdrawals).Int()
if err != nil {
return series.Ints("NAN")
}
return series.Ints(deposit - withdrawal)
})
// The new series is appended to
// the data source via a call to Mutate.
// You can print s to read its content.
df = df.Mutate(s.Col("X0")).
Rename("deposit_Withdrawals_diff", "X0")
fmt.Println(df)
}
but the question is that, I want to add an index ( row counter) to each row. ( later on I want to join it with a subset of that) So I need an index.
something like
index,accountId,deposit,Withdrawals
1,anil0001,50,10
2,vikas0002,10,10
3,ravi0003,20,10
4,user1111,NaN,20
I see there are no GetIndex or Index methods on series. How can I add this index?
I did it with a global variable ( but I'm not sure it's the best solution for gota, maybe for pure go developer is a good solution :) )
index := 0
s := df.Rapply(func(s series.Series) series.Series {
index++
return series.Ints(index)
})
df = df.Mutate(s.Col("X0")).
Rename("index", "X0")
fmt.Println(df)
I'm currently trying to modify an element of a slice by passing a pointer to a function. Outside of the function the element is not modified.
Is there a way to modify the element without passing the slice itself along with the index of the desired element to alter?
package main
import (
"fmt"
)
type Item struct {
Value int
}
func alter(t *Item) {
(*t).Value = 100
}
func main() {
items := []Item{Item{0}, Item{1}}
for _, item := range items {
alter(&item)
}
fmt.Println(items) // Output is still [{0} {1}]
}
Golang Playground
for i := range items {
alter(&items[i])
}
Or
items := []*Item{{0}, {1}}
for _, item := range items {
alter(item)
}
The reason your version doesn't work is because the iteration variable item holds a copy of the element inside the slice, which means that what you're modifying is the copy and not the original. You can see that they are separate objects in memory if you run this: https://play.golang.org/p/vr9CfX0WQcB
Refer: https://tour.golang.org/moretypes/16
The range form of the for loop iterates over a slice or map.
When ranging over a slice, two values are returned for each iteration. The first is the index, and the second is a copy of the element at that index.
So,
for i, x := range arr {
// x is copy for arr[i]
}
Hence, we will directly used arr[i] and pass the address of the same to the alter function so that it could be modified.
Sample code:
package main
import "fmt"
type Item struct {
Value int
}
func alter(t *Item) {
(*t).Value = 100
}
func main() {
items := []Item{{0}, {1}}
for i := range items {
alter(&items[i])
}
fmt.Println(items)
}
[Playground]
Assume I have []struct{} and I need to know whether an element with id = A exists in the slice. If exists, the element will be removed or moved to index 0 according to request in user input. So, how to find an element in golang slice in optimal way without check each element? Or, is using slice.contains(obj) enough? Then, if the element exists, I will do action according to request in user input. If the request is remove, I will remove it without broke the elements order. But if the request is add, I will move the element to index 0.
Note: The function will be often called.
Thank you.
It is not difficult to write function to find element by iterating over slice:
func contains(s []your_struct, e int) (bool, int) {
for idx, a := range s {
if a.id == e {
return true, idx
}
}
return false, -1
}
If you a going to call the function often it may be useful to sort the slice by id field and implement binary search over slice of your_struct.
If the slice is not very big you can create additional data structure - map[int]int and keep the indexes of elements of the slice in this map. But in this case you need to synchronize content of your slice and the map when you are modifying one of them:
your_map := make(map[int]int)
if idx, ok := your_map[id]; ok {
// ...
}
If you need to check many times then
it's better to create a map[string]int of id field one time.
And every time just check map contains that id or not
Here,id as key and slice index as value
mp := make(map[string]int)
for idx, a := range yourStuctSlice {
mp[a.id] = idx
}
if idx, ok := mp[id]; ok {
// remove the element using idx
}
If new element added in slice then update the map also
mp[newElement.id] = true
If you want to remove searched element you can remove by slice index
func RemoveIndex(s []yourStuct, index int) []int {
return append(s[:index], s[index+1:]...)
}
if idx, ok := mp[id]; ok {
yourStuctSlice = RemoveIndex(yourStuctSlice , idx)
delete(mp , id); // Remove from map also for next search
}
I have the code below
teasers := []*models.TeaserCount{}
var teaser models.TeaserCount
for _, product := range ProductResponse.Products {
added := false
if len(product.Campaign.Id) > 0 {
if len(teasers) > 0 {
for _, teaserCount := range teasers {
if teaserCount.Id == product.Campaign.Id {
fmt.Println(teaserCount.Id, teaserCount.Count+1)
teaserCount.Count++
added = true
break
}
}
if added == false {
teaser = models.TeaserCount{
Id: product.Campaign.Id,
Count: 0,
}
teasers = append(teasers, &teaser)
}
} else {
teaser = models.TeaserCount{
Id: product.Campaign.Id,
Count: 0,
}
teasers = append(teasers, &teaser)
}
}
}
What I want to do, is to count how many times each campaign occured in product
I want to have an array of objects including campaign id and occurences
Results that I get is that each and every single object in array is the same(the last one added by append)
How is that so, the behavior seems quite strange to me, maybe that has to do with pointers?
You're appending a pointer to the local loop variable, which changes on each iteration:
// This pointer will always point to the current/last loop iteration value
teasers = append(teasers, &teaser)
You should instead either append a pointer to a copy:
temp := teaser
teasers = append(teasers, &temp)
Or a pointer to the element of the slice:
for i, product := range ProductResponse.Products {
// ...
teasers = append(teasers, &ProductResponse.Products[i])
If you choose the former, the pointer will be to a copy dedicated to teasers, whereas if you do the latter, it will be a pointer to the element of the original slice (meaning if the value in the slice changes, that will be reflected in teasers).
I have the following code that adds a new element to a slice if it doesnt exist already. If it does exist then the qty property should be incremented of the existing element instead of a new element being added:
package main
import (
"fmt"
)
type BoxItem struct {
Id int
Qty int
}
type Box struct {
BoxItems []BoxItem
}
func (box *Box) AddBoxItem(boxItem BoxItem) BoxItem {
// If the item exists already then increment its qty
for _, item := range box.BoxItems {
if item.Id == boxItem.Id {
item.Qty++
return item
}
}
// New item so append
box.BoxItems = append(box.BoxItems, boxItem)
return boxItem
}
func main() {
boxItems := []BoxItem{}
box := Box{boxItems}
boxItem := BoxItem{Id: 1, Qty: 1}
// Add this item 3 times its qty should be increased to 3 afterwards
box.AddBoxItem(boxItem)
box.AddBoxItem(boxItem)
box.AddBoxItem(boxItem)
fmt.Println(len(box.BoxItems)) // Prints 1 which is correct
for _, item := range box.BoxItems {
fmt.Println(item.Qty) // Prints 1 when it should print 3
}
}
The problem is that the qty is never incremented correctly. It always ends in 1 when it should be 3 in the example provided.
I have debugged the code and it does appear that the increment section is reached but the value just isnt persisted to the item.
What is wrong here?
You are incrementing Qty in the copy of the box.BoxItems because range will yield the copy of the elements in the slice. See this example.
So, in for _, item := range box.BoxItems, item is a copy of of the elements in box.BoxItems.
Change your loop to
for i := 0; i < len(box.BoxItems); i++ {
if box.boxItems[i].Id == boxItem.Id {
box.boxItems[i].Qty++
return box.BoxItems[i]
}
}
Playground
I will answer your question pretty much like others have done. However, not that the problem you try to solve is not best served by looping over a range of values. Read on:
Solution to your question
Like others have said, for-range provide an immutable iteration over the range of values. That means any change you make to the value provided in the iteration will be lost. It's basically giving you a copy of the real value, not the actual value.
for _, item := range box.BoxItems {
// ^-not the real `item`, it's a copy!
A way around this is to keep track of the indexing value in the for idx, val := range, and use this idx to address the value you look for directly.
If you change your for-loop to keep the index value:
for i, item := range box.BoxItems {
// ^-keep this
You will be able to reference the actual item in the array you loop on:
for i, item := range box.BoxItems {
// Here, item is a copy of the value at box.BoxItems[i]
if item.Id == boxItem.Id {
// Refer directly to an item inside the slice
box.BoxItems[i].Qty++
return box.BoxItems[i] // Need to return the actual one, not the copy
}
}
Playground
I would favor this approach over the for i; i<Len; i++ one as I find it more readable. But this is simply a matter of taste and the for i form will be more efficient (beware of premature-optimization!).
Your real problem is
What you're trying to do is to avoid duplicating BoxItems if their Id already exists. To do this, you iterate over the whole range of the box.BoxItems slice. If you have N items in your box.BoxItems slice, you will potentially iterate over all N items before finding out that the item you're looking for doesn't exist! Basically, this means your algorithm is O(N).
If you increment Id in natural order
That is, 0, 1, 2, 3, ..., n - 1, n, you can keep using a slice to index your box items. You would do like this:
func (box *Box) AddBoxItem(boxItem BoxItem) BoxItem {
// Lookup your item by Id
if boxItem.Id < len(box.BoxItems) {
// It exists, do don't create it, just increment
item := box.BoxItems[boxItem.Id]
item.Qty++
box.BoxItems[boxItem.Id] = item
return item
}
// New item so append
box.BoxItems = append(box.BoxItems, boxItem)
return boxItem
}
Playground
If you increment Id in any order
You should use a datastructure that offers fast lookups, such as the built-in map, which offers O(1) lookups (that means, you need to do a single operation to find your item, not n operations).
type Box struct {
BoxItems map[int]BoxItem
}
func (box *Box) AddBoxItem(boxItem BoxItem) BoxItem {
// Lookup the map by Id
item, ok := box.BoxItems[boxItem.Id]
if ok {
// It exists, do don't create it, just increment
item.Qty++
} else {
item = boxItem
}
// New item so add it to the map
box.BoxItems[boxItem.Id] = item
return item
}
Playground
This is a more correct way to solve your problem.
In the index, value := range someSlice, the value is a fresh new copy of someSlice[index].
package main
import (
"fmt"
)
type BoxItem struct {
Id int
Qty int
}
type Box struct {
BoxItems []BoxItem
}
func (box *Box) AddBoxItem(boxItem BoxItem) BoxItem {
// If the item exists already then increment its qty
for i := range box.BoxItems {
item := &box.BoxItems[i]
if item.Id == boxItem.Id {
item.Qty++
return *item
}
}
// New item so append
box.BoxItems = append(box.BoxItems, boxItem)
return boxItem
}
func main() {
boxItems := []BoxItem{}
box := Box{boxItems}
boxItem := BoxItem{Id: 1, Qty: 1}
// Add this item 3 times its qty should be increased to 3 afterwards
box.AddBoxItem(boxItem)
box.AddBoxItem(boxItem)
box.AddBoxItem(boxItem)
fmt.Println(len(box.BoxItems)) // Prints 1 which is correct
for _, item := range box.BoxItems {
fmt.Println(item.Qty) // Prints 1 when it should print 3
}
}
Playground
Output:
1
3