Clueless moron in need of pointers with synax

polaris · · 195 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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(&#34;^~b/o ([0-9\\.]+) ([^ ]+)$&#34;) matches := re.FindStringSubmatch(item.Note) if matches != nil { fmt.Printf(&#34;count = %v, currency = %v&#34;, matches[1], matches[2]) </code></pre> <p>With this</p> <pre><code>func main() { log.Printf(&#34;requesting a recent change id from;) recentChangeId, err := getRecentChangeId() if err != nil { log.Fatal(err) } log.Printf(&#34;starting with change id %v&#34;, 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) &lt;-ch log.Printf(&#34;shutting down&#34;) subscription.Close() }() // Loop forever over results. for result := range subscription.Channel { if result.Error != nil { log.Printf(&#34;error: %v&#34;, result.Error.Error()) continue } for _, stash := range result.PublicStashTabs.Stashes { processStash(&amp;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 == &#34;Leaguename&#34; &amp;&amp; item.Type == &#34;Itemname&#34; { log.Printf(&#34;Itemname: account = %v, league = %v, note = %v, tab = %v&#34;, 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 &#34;undefined: regexp in regexp.MustCompile&#34;</p> <pre><code>func processStash(stash *api.Stash) { re := regexp.MustCompile(&#34;^~b/o ([0-9\\.]+) ([^ ]+)$&#34;) for _, item := range stash.Items { matches := re.FindStringSubmatch(item.Note) if item.League == &#34;Leaguename&#34; &amp;&amp; item.Type == &#34;Itemname&#34; &amp;&amp; matches != nil { log.Printf(&#34;item name: account = %v, league = %v, note = %v, tab = %v&#34;, 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 ( &#34;encoding/json&#34; &#34;io/ioutil&#34; &#34;log&#34; &#34;net/http&#34; &#34;os&#34; &#34;os/signal&#34; &#34;; ) </code></pre></pre>SilentWeaponQuietWar: <pre><p>right, so you need to add &#34;regexp&#34; in there</p></pre>NeverUse-YouPromised: <pre><p>I&#39;m not sure what you mean by &#34;combine&#34;. If you could say what you&#39;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 &#39;func main&#39;. 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

195 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传