#How do I scan a string with spaces into one variable?

27 messages · Page 1 of 1 (latest)

lapis crescent
#

And on top of that, how would I scan two newline separated sentences into two variables?
I know this is a super beginner question but I have looked everywhere without answer.

#

I will use other libraries if need be, but just fmt would be preferable.

robust fossil
#

it's in the stdlib so also not an outside package or anything

lapis crescent
#

cool, how do I get the string in the first place?

#

I'm fine with seperating at newline

robust fossil
#

Oh like from the user in the terminal

lapis crescent
#

yeah

robust fossil
#

So u do like

func getname(){
  var name string
  fmt.Println("Enter your name:") 
  fmt.Scan(&name)
}
#

Do you know what a pointer is?

lapis crescent
#

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

robust fossil
#

if you just did fmt.Scan(&s1,&s2) what happens

lapis crescent
#

I don't remember, one sec

robust fossil
lapis crescent
lapis crescent
robust fossil
#

Scanln?

lapis crescent
#

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

robust fossil
#
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')

lapis crescent
#

Sure, I'll check it out