Hi guys. As title said I am trying to alter some Go code for my own evil means. I have had a look over some literature but still not much the wiser so I am in need of help. Easy I am sure for anyone who is fam... almost anyone but me.
Here is the code. I am trying to combine this
re := regexp.MustCompile("^~b/o ([0-9\\.]+) ([^ ]+)$")
matches := re.FindStringSubmatch(item.Note)
if matches != nil {
fmt.Printf("count = %v, currency = %v", matches[1], matches[2])
With this
func main() {
log.Printf("requesting a recent change id from xyz.ninja...")
recentChangeId, err := getRecentChangeId()
if err != nil {
log.Fatal(err)
}
log.Printf("starting with change id %v", recentChangeId)
subscription := api.OpenPublicStashTabSubscription(recentChangeId)
// If we get an interrupt signal (e.g. from control+c on the terminal), handle it gracefully.
go func() {
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt)
<-ch
log.Printf("shutting down")
subscription.Close()
}()
// Loop forever over results.
for result := range subscription.Channel {
if result.Error != nil {
log.Printf("error: %v", result.Error.Error())
continue
}
for _, stash := range result.PublicStashTabs.Stashes {
processStash(&stash)
}
}
}
My syntax always seems wrong. I even spelt syntax wrong in the title, that is how bad I am!
评论:
throwawayguy123xd:
Silicium_Avatara:you have to put it wherever you can access the variable
item
; which isnt in main() at all so youll have to add another lineit should look like this
main(){ step1.. step2... item := howIGetItem() newcode... step4...
so you can always tell where to put it by what variables the code needs
in this code it says it loops forever, so thats something to consider. that usually means it has to go in two places, 1) in the for loop, or 2) right after the inner-most for-loop, but still in the forever loop
third option is, if variable
item
comes out of the variablestash
you would put it the new code insideprocessStash
or afterprocessStash(stash)
gl.
Silicium_Avatara:Oh I see yes, sorry for making things confusing, your explanation was spot on. Here is the function I should have been altering with the new code.
func processStash(stash *api.Stash) { for _, item := range stash.Items { if item.League == "Leaguename" && item.Type == "Itemname" { log.Printf("Itemname: account = %v, league = %v, note = %v, tab = %v", stash.AccountName, item.League, item.Note, stash.Label) } }
}
I will take another look at it later when I get a bit more time.
SilentWeaponQuietWar:Well I had a look and tried but I am getting an error message "undefined: regexp in regexp.MustCompile"
func processStash(stash *api.Stash) { re := regexp.MustCompile("^~b/o ([0-9\\.]+) ([^ ]+)$") for _, item := range stash.Items { matches := re.FindStringSubmatch(item.Note) if item.League == "Leaguename" && item.Type == "Itemname" && matches != nil { log.Printf("item name: account = %v, league = %v, note = %v, tab = %v", stash.AccountName, item.League, item.Note, stash.Label) } } }
Silicium_Avatara:you are likely missing the regexp package in your imports
SilentWeaponQuietWar:my imports are
import ( "encoding/json" "io/ioutil" "log" "net/http" "os" "os/signal" "github.com/ccbrown/poe-go/api" )
NeverUse-YouPromised:right, so you need to add "regexp" in there
Silicium_Avatara:I'm not sure what you mean by "combine". If you could say what you're trying to do that would help.
ChristophBerger:Sorry yes, what would be the correct way to insert the smaller section of code into the larger section of code. I am sure it goes in 'func main'. It performs the function of filtering the results.
I am sure it is simple I know but my formatting is terrible. This is why I hate coding, everything has to be perfect. If I could just tell it what I wanted it to do then I would have a great time. No J.A.R.V.I.S yet though.
If you hate coding, why are you doing it? Is this perhaps a homework assignment? ;-)
Your description is still quite vague. Where do you want to place the code snippet into main? What syntax error message do you get?
