2014-06-03

Go's Scanln (GoLang)

So.. Today I tried to study about Go, programming language by Google, that i expect would be good replacement for C/C++/Java..

Instalation? easy~
sudo pacman -S go # ArchLinux
go version # go version go1.2.2 linux/amd64
running? easy~
go build try1.go && ./try1 # or
go run try1.go
input and output? easy~ ^^)b
package main
import (
 "bufio"
 "fmt"
 "os"
)
func main(){
  var i int
  var f float64
  var s string
  r := bufio.NewReader(os.Stdin)
  _, err := fmt.Scan(&i,&f,&s)
  if err == nil {
   fmt.Println("read int, float and string:",i,f,s)
  } else {
   fmt.Println("Error:",err)
  }
  s, err = r.ReadString('\n')
  if err == nil {
   fmt.Println("read line:",s)
  } else {
   fmt.Println("Error:",err)
  }
}

 Just some tricy part, I expect that fmt.Scanln would behave like C++'s getline(cin,str) or Java's Scanner scan.nextLine(), but it doesn't.. oh well..

No comments:

Post a Comment

THINK: is it True? is it Helpful? is it Inspiring? is it Necessary? is it Kind?