multiple-value reader.ReadString() in single-value context [duplicate] - go

This question already has answers here:
Multiple values in single-value context
(6 answers)
Closed 4 years ago.
Go newbie here.
I am trying to run very simple example on go1.11.4 windows/amd64
Here is my code below;
sandbox: https://play.golang.org/p/GoALi4HYx3L
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
fmt.Print("Enter a grade: ")
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
input := reader.ReadString('\n')
fmt.Println(input)
}
And I am getting following error:
prog.go:13:28: multiple-value
reader.ReadString() in single-value context
Am I missing something here?

Check the documentation for ReadString, especially the part that describes return values (Tip: it is in the title of the section).
https://golang.org/pkg/bufio/#Reader.ReadString
Also, it worse check this reading also
https://gobyexample.com/multiple-return-values

Related

Golang r.URL.Query("x") does not return the full value with special characters [duplicate]

This question already has answers here:
Characters allowed in GET parameter
(7 answers)
Closed 9 months ago.
I was using r.URL.Query("model") to retrieve a url param. Lets say "https://example.com?model=345;1"
My expected behaviour is to retrieve: 345;1
But I only receive: 345
Is there a specific meaning behind it? and how i can force to get the full value. I am quite new go.
Code example
As LeGEC indicated the problem is due the fact that the semi-colon (;) is a character that has (or could have) special meaning in URLs.
You can use "%#v" in your Go Printf example to see how Go parsed the query string:
package main
import "fmt"
import "net/url"
import "log"
func main() {
u, err := url.Parse("https://example.com?model=345;1")
if err != nil {
log.Fatal(err)
}
q := u.Query()
fmt.Printf("%#v",q)
}
Gives
url.Values{"1":[]string{""}, "model":[]string{"345"}}
You could use u.RawQuery to get the entire string "model=345;1" and then parse it yourself. Not ideal, but perhaps a quick workaround.

What is wrong with this go code, and what is os.Stdin? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
1 package main
2
3 import (
4 "bufio"
5 "fmt"
6 "os"
7 )
8
9 func main() {
10 input := bufio.NewScanner(os.Stdin)
11 if input.Scan == 1 {
12 fmt.println("true")
13 }
14 }
I want create something that will ask for user input, then check if that user input = 1
The Scan code documentation says:
//Scan advances the Scanner to the next token, which will then be
//available through the Bytes or Text method. It returns false when the
//scan stops, either by reaching the end of the input or an error.
So you could do something like this:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
input := bufio.NewScanner(os.Stdin)
if input.Scan() && input.Text() == "1" {
fmt.Println("true")
}
}
The os.Stdin is how you make your Scanner get it's input from the stdin.
(https://en.wikipedia.org/wiki/Standard_streams#/media/File:Stdstreams-notitle.svg)
One note, pay attention for uppercase letters for exported functions.
On line 12 you wrote
fmt.println
and it should be
fmt.Println
You should go to
https://tour.golang.org/welcome/1
to get started with golang.

How do I make a GO program wait until there is user input? [duplicate]

This question already has answers here:
How can I read from standard input in the console?
(12 answers)
Closed 7 years ago.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
fmt.Println("insert y value here: ")
input := bufio.NewScanner(os.Stdin)
fmt.Println(input.Text)
}
How do I make the program wait, until the user inputs data?
Scanner isn't really ideal for reading command line input (see the answer HectorJ referenced above), but if you want to make it work, it's a call to Scan() that you're missing (also note that Text() is a method call):
func main() {
fmt.Print("insert y value here: ")
input := bufio.NewScanner(os.Stdin)
input.Scan()
fmt.Println(input.Text())
}

How to get executing code's file's name in go? [duplicate]

This question already has answers here:
Is there a way to get the source code filename and line number in Go?
(1 answer)
Find the path to the executable
(3 answers)
Closed 7 years ago.
Say I have a file:
i_want_this_name.go:
package main
func main(){
filename := some_func() // should be "i_want_this_name"
}
How do I get the executing code's file's name in go?
The name of the command can be found in os.Args[0] as states in the documentation for the os package:
var Args []string
Args hold the command-line arguments, starting with the program name.
To use it, do the following:
package main
import "os"
func main(){
filename := os.Args[0]
}
This should work for you:
package main
import (
"fmt"
"runtime"
)
func main() {
_, fileName, lineNum, _ := runtime.Caller(0)
fmt.Printf("%s: %d\n", fileName, lineNum)
}

How to differentiate empty string and nothing in a map [duplicate]

This question already has answers here:
How to check if a map contains a key in Go?
(11 answers)
Closed 7 years ago.
The following code yields true. So I'm wondering for map[string]string in Golang, is there a way to differentiate empty string and nothing?
package main
import "fmt"
func main() {
m := make(map[string]string)
m["abc"] = ""
fmt.Println(m["a"] == m["abc"]) //true
}
If by "nothing" you mean that the element is not in the map you can use the ok idiom:
val, ok := myMap["value"] // ok is true if value was in the map
You can find more information in Effective Go.

Resources