bhawani shanker
bhawani shanker

Reputation: 79

Go Program not working in WIndows 7

I am using this program to create a single linked list and print elements of list. Its working in my Mac but when I try same program in Windows 7 its not working as expected. Can someone identify what is issue here?

// Create a single linked list and display elements of list
package main

import "fmt"

// Node structure
type Node struct {
    Value int
    Next *Node
}

func main() {
    var value int
    var head, current *Node

    // Create linked list
    for i := 0; i <= 5; i++ {
       fmt.Print("Plase enter a number: ")
       fmt.Scanf("%d", &value)

       var newNode = &Node{value, nil}
       if head != nil {
          current.Next = newNode
       } else {
          head = newNode
       }
       current = newNode
    }

    // Print elements of linked list
    for node := head; node != nil; node = node.Next {
        fmt.Printf("%d ", node.Value)
    }
}

Output

E:\go > go run linked_list.go
Please enter a number: 10
Please enter a number: Please enter a number: 20
Please enter a number: Please enter a number: 30
Please enter a number: 10 20 20 30 30 30

Upvotes: 0

Views: 207

Answers (1)

MahlerFive
MahlerFive

Reputation: 5279

It looks like Scanf is treating windows newlines (\r\n) differently than unix newlines (\n). I believe this was a bug in older versions of Go. What version are you running? Try using 1.7 or later.

As a workaround, try doing fmt.Scanf("%d\n", &value) to explicitly eat a newline character.

Upvotes: 2

Related Questions