#How do I scan a string with spaces into one variable?
27 messages · Page 1 of 1 (latest)
strings.Split is what ur lookin for
it's in the stdlib so also not an outside package or anything
cool, how do I get the string in the first place?
I'm fine with seperating at newline
Oh like from the user in the terminal
yeah
So u do like
func getname(){
var name string
fmt.Println("Enter your name:")
fmt.Scan(&name)
}
Do you know what a pointer is?
yeah, vaguely
in this case, would the first name not just fill name?
Right now I have
var s1, s2 string
fmt.Scanf("%v\n%v",&s1,&s2)```
and it just doesn't take in the full sentence
if you just did fmt.Scan(&s1,&s2) what happens
I don't remember, one sec
what do you mean by this? try to rephrase. I'm having trouble understanding what you mean.
on hitting enter, the first word becomes s1, and the second s2.
Let's say you type in "Konnhor Khliyn", name becomes "Konnhor", because it stops scanning at a space
Scanln?
same effect
I have actually no clue how to do this
Welp, I'll figure it out later. Thanks for the help. Google is being intensely unhelpful
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Enter your first name:")
firstName, _ := reader.ReadString('\n')
fmt.Println("Enter your last name:")
lastName, _ := reader.ReadString('\n')
fmt.Printf("Hello, %s %s!\n", firstName, lastName)
}
``` could this be it
bufio is also in std as u can see by the fact that u don't have to go get anything
also firstName and lastName still contain the newline character ('\n')
Sure, I'll check it out