<p>Hi <a href="/r/golang" rel="nofollow">r/golang</a>,</p>
<p>So this probably gets asked a ton and I am still getting my sea legs here in the land of go. As a prolific googler I try to find everything first and read several posts first and have learned quite a bit already. I am not sure if Marshaling or Unmarshaling<br/>
or simplejson/djson/(insertjsonlibhere) are the way to go here.</p>
<p>I have a program that can read from a json file and return the information formatted. but I am also trying to parse and return array data based on input. Honestly I am lost here and I don't have a good way to form the question as everything I find is marshal/unmarshal but not parse and return clear text strings. Honestly this will help with 2 projects that I have so that is a thing as well.</p>
<p>Here is my current work<br/>
<a href="https://gist.github.com/parkervcp/2d9e42f571170b523b6cc70d9528d318" rel="nofollow">https://gist.github.com/parkervcp/2d9e42f571170b523b6cc70d9528d318</a></p>
<hr/>**评论:**<br/><br/>GoHomeGrandmaUrHigh: <pre><p>Your code gets pretty close. After unmarshalling the JSON, you can just iterate through it as a normal Go struct. </p>
<pre><code>func main() {
// Read the line from standard input
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
fmt.Println(text)
commands := getCommands()
for _, p := range commands {
if p.Cmd == text {
for _, line := range p.Lns {
fmt.Println(line)
}
}
}
}
</code></pre></pre>parkervcp: <pre><p>I am trying this and I only get my input as a response.</p>
<pre><code>Enter text: Hello!
Hello!
</code></pre></pre>GoHomeGrandmaUrHigh: <pre><p>I adapted your code snippet for the Go Playground: <a href="https://play.golang.org/p/FDUD8IZ1io" rel="nofollow">https://play.golang.org/p/FDUD8IZ1io</a></p>
<p>Changes:</p>
<ul>
<li>In your <code>getCommands()</code> I hard-coded the "file contents" (copy/pasted your JSON inputs)</li>
<li>In main() I hard-coded the value for "text" because no standard input on the playground.</li>
</ul>
<p>With <code>text := "test2"</code>, I got as output:</p>
<pre><code>This is one line
this is another
Program exited.
</code></pre>
<p>The other values "test1" and "hello" got me the expected outputs for those, too. Invalid values just didn't print anything, which makes sense because none of the <code>p.Cmd</code> was equal to it.</p></pre>morethanaprogrammer: <pre><p>If you are looking for a constant time lookup of the replies to a particular command, you could use a map, like so: <a href="https://play.golang.org/p/RXWZCFqYHf" rel="nofollow">https://play.golang.org/p/RXWZCFqYHf</a></p></pre>parkervcp: <pre><p>Reason I have the separate commands file it to have it easily modifiable by others than me.</p>
<p>edit: this actually might be exactly what I was looking for though.</p></pre>morethanaprogrammer: <pre><p>You can still have the separate commands file. I was showing how you could do a lookup by command name.</p></pre>parkervcp: <pre><p>Working on that now.</p></pre>