<p>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.</p>
<p>Here is the code. I am trying to combine this</p>
<pre><code>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])
</code></pre>
<p>With this</p>
<pre><code>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)
}
}
}
</code></pre>
<p>My syntax always seems wrong. I even spelt syntax wrong in the title, that is how bad I am!</p>
<hr/>**评论:**<br/><br/>throwawayguy123xd: <pre><p>you have to put it wherever you can access the variable <code>item</code>; which isnt in main() at all so youll have to add another line</p>
<p>it should look like this</p>
<pre><code>main(){
step1..
step2...
item := howIGetItem()
newcode...
step4...
</code></pre>
<p>so you can always tell where to put it by what variables the code needs</p>
<p>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</p>
<p>third option is, if variable <code>item</code> comes out of the variable <code>stash</code> you would put it the new code inside <code>processStash</code> or after <code>processStash(stash)</code></p>
<p>gl.</p></pre>Silicium_Avatara: <pre><p>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.</p>
<pre><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)
}
}
</code></pre>
<p>}</p>
<p>I will take another look at it later when I get a bit more time.</p></pre>Silicium_Avatara: <pre><p>Well I had a look and tried but I am getting an error message "undefined: regexp in regexp.MustCompile"</p>
<pre><code>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)
}
}
}
</code></pre></pre>SilentWeaponQuietWar: <pre><p>you are likely missing the regexp package in your imports</p></pre>Silicium_Avatara: <pre><p>my imports are </p>
<pre><code>import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"github.com/ccbrown/poe-go/api"
)
</code></pre></pre>SilentWeaponQuietWar: <pre><p>right, so you need to add "regexp" in there</p></pre>NeverUse-YouPromised: <pre><p>I'm not sure what you mean by "combine". If you could say what you're trying to do that would help.</p></pre>Silicium_Avatara: <pre><p>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.</p>
<p>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.</p></pre>ChristophBerger: <pre><p>If you hate coding, why are you doing it? Is this perhaps a homework assignment? ;-)</p>
<p>Your description is still quite vague. Where do you want to place the code snippet into main? What syntax error message do you get?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传