I'm new in Go and writing a simple insertion sort but when i change my fileName to "insertion.go" I got error:
invalid identifier character U+00A0 at insertion.go:2:1
but when I change the filename to anything else it works fine:
insertion.go
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
slice := generateSlice(20)
fmt.Println("
--- Unsorted ---
", slice)
insertionsort(slice)
fmt.Println("
--- Sorted ---
", slice, "
")
}
func generateSlice(size int) []int {
slice := make([]int, size, size)
rand.Seed(time.Now().UnixNano())
for i := 0; i < size; i++ {
slice[i] = rand.Intn(999) - rand.Intn(999)
}
return slice
}
func insertionsort(items []int) {
var n = len(items)
for i := 1; i < n; i++ {
j := i
for j > 0 {
if items[j-1] > items[j] {
items[j-1], items[j] = items[j], items[j-1]
}
j = j - 1
}
}
}
I want to know what's the problem with name "insertion"?
Please read the error carefully. Look where the error is occurring: line 2, character 1. That means you have some invalid character in your source file. It's not a problem with the filename. U+00A0
is a NO-BREAK SPACE, meaning you have an invalid no-break space character in your file, which will appear as invisible.
So apparently, you have the following contents:
package main
X
import (
Where X
is an (invisible) NO-BLOCK SPACE (aka
in HTML speak).
A guess would be that perhaps you copy-pasted this code from a web site, and a
was erroneously included in the paste.