Hello World - unexpected NUL in input

3,902 views
Skip to first unread message

john.me...@gmail.com

unread,
Jan 5, 2014, 6:30:20 AM1/5/14
to golan...@googlegroups.com
Dear all,

I run into this error with my first go app. Would anyone please give me some tips?

Error 
W:\source\go\src>go install github.com\user\hello

can't load package: package github.com/user/hello: read W:\source\go\src\github.
com\user\hello\hello.go: unexpected NUL in input

Source
// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func main() {
    fmt.Println("Hello")
}

Thanks,
John

Dave Cheney

unread,
Jan 5, 2014, 5:46:25 PM1/5/14
to john.me...@gmail.com, golan...@googlegroups.com
Try deleting everything before the package decl. 
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

John Melvic

unread,
Jan 5, 2014, 6:51:32 PM1/5/14
to golan...@googlegroups.com, john.me...@gmail.com
I changed the code to the following & it didn't work.

package main

import "fmt"

func main() {
fmt.Printf("Hello, world.\n")
}

I suspect it has something to do with the folder structure.

OS: Windows
GOPATH=W:\tools\go
GOROOT=W:\source\go
Folders
   W:\source
   W:\source\go\bin
   W:\source\go\pkg
   W:\source\go\src
   W:\source\go\src\hello
   W:\source\go\src\hello\hello.go
Command: 
W:\source\go>go install hello
can't load package: package hello: cannot find package "hello" in any of:
        W:\source\go\src\pkg\hello (from $GOROOT)
        W:\tools\go\src\hello (from $GOPATH)

Because of the pkg error, I moved the structure around to 
   W:\source
   W:\source\go\bin
   W:\source\go\pkg
   W:\source\go\src
   W:\source\go\src\pkg\hello
   W:\source\go\src\pkg\hello\hello.go

W:\source\go>go install hello
can't load package: package hello: read W:\source\go\src\pkg\hello\hello.go: unexpected NUL in input

W:\source>go install hello
can't load package: package hello: read W:\source\go\src\pkg\hello\hello.go: unexpected NUL in input

W:\>go install hello
can't load package: package hello: read W:\source\go\src\pkg\hello\hello.go: unexpected NUL in input

Dave Cheney

unread,
Jan 5, 2014, 6:54:44 PM1/5/14
to John Melvic, golang-nuts
I think you have GOPATH and GOROOT mixed up.


Jesse McNelis

unread,
Jan 5, 2014, 7:08:04 PM1/5/14
to John Melvic, golang-nuts
On Mon, Jan 6, 2014 at 10:51 AM, John Melvic <john.me...@gmail.com> wrote:
I changed the code to the following & it didn't work.

package main

import "fmt"

func main() {
fmt.Printf("Hello, world.\n")
}
 
W:\source\go>go install hello
can't load package: package hello: read W:\source\go\src\pkg\hello\hello.go: unexpected NUL in input

The problem is that somewhere in your hello.go is a ASCII NULL character. 
It's likely the result of some kind of text encoding issue with either your text editor or whatever program you copied the code from.
All Go code needs to be UTF8, if your text editor is using some other encoding then you'll have problems like this.
The simplest thing to do in this case is just delete the code and retype it.


--
=====================
http://jessta.id.au

John Melvic

unread,
Jan 5, 2014, 7:15:05 PM1/5/14
to golan...@googlegroups.com, John Melvic, jes...@jessta.id.au
It works after I changed it to UTF8 from Unicode!  Thank you Jesse!

Daniel Theophanes

unread,
Jan 5, 2014, 7:53:05 PM1/5/14
to golan...@googlegroups.com, John Melvic, jes...@jessta.id.au
As a side note: Windows has funny (somewhat inaccurate) names:

Both UTF-8 and UTF-16 formats use unicode code points. Windows calls UTF-16 LE "Unicode". As go source code uses UTF-8, typical ASCII range characters are 1 byte each
'h' 'e' 'l' 'l' 'o'
while UTF-16 are two bytes each:
0 'h' 0 'e' 0 'l' 0 'l' 0 'o'

Thus because the Go compiler expects UTF-8, it interperts the UTF-16 zero bytes as invalid NULL characters.
-Daniel

John Melvic

unread,
Jan 5, 2014, 8:01:58 PM1/5/14
to golan...@googlegroups.com, John Melvic, jes...@jessta.id.au
Someone should put this in official go document. Is there anyway I could update it?  It is owned by Google...I assume not?

Jesse McNelis

unread,
Jan 5, 2014, 8:26:36 PM1/5/14
to John Melvic, golang-nuts
On Mon, Jan 6, 2014 at 12:01 PM, John Melvic <john.me...@gmail.com> wrote:
Someone should put this in official go document. Is there anyway I could update it?  It is owned by Google...I assume not?


"Source code is Unicode text encoded in UTF-8. The text is not canonicalized, so a single accented code point is distinct from the same character constructed from combining an accent and a letter; those are treated as two code points. For simplicity, this document will use the unqualified term character to refer to a Unicode code point in the source text." 


--
=====================
http://jessta.id.au

Ian Lance Taylor

unread,
Jan 5, 2014, 9:45:05 PM1/5/14
to John Melvic, golang-nuts, Jesse McNelis
On Sun, Jan 5, 2014 at 5:01 PM, John Melvic <john.me...@gmail.com> wrote:
> Someone should put this in official go document. Is there anyway I could
> update it? It is owned by Google...I assume not?

The official Go document is found in the source code at
doc/go_spec.html. It can be updated like any other part of the Go
distribution.

There is no need to update it for this, though; as Jesse McNelis
pointed out it's already there.

Ian

John Melvic

unread,
Jan 6, 2014, 5:14:32 AM1/6/14
to golan...@googlegroups.com, John Melvic, jes...@jessta.id.au
You guys are awesome!  Thank you all!

raphae...@gmail.com

unread,
Mar 29, 2018, 12:01:55 AM3/29/18
to golang-nuts
Hi,

Sorry to revive an age old thread, but I'm encountering this error and it seems my files are already utf-8:

$ file -I main.go 

main.go: text/x-c; charset=utf-8

$ file -I cmd/root.go 

cmd/root.go: text/x-c; charset=utf-8


Is there any programmatic way to detect the messed up character? Possibly this is not the exact issue, because my file compiles on mac but not linux.

Best wishes,
Raphael Deem

Volker Dobler

unread,
Mar 29, 2018, 2:02:23 AM3/29/18
to golang-nuts
$ od -ta main.go | grep nul
might work if NUL is the problem.

V.

Wojciech S. Czarnecki

unread,
Mar 29, 2018, 5:30:48 AM3/29/18
to golan...@googlegroups.com
On Wed, 28 Mar 2018 20:05:34 -0700 (PDT)
raphae...@gmail.com wrote:

> Is there any programmatic way to detect the messed up character? Possibly
> this is not the exact issue, because my file compiles on mac but not linux.

on linux you may use iconv tool:

$ iconv -f UTF8 file.go 2>&1 | tail -1

It will exit on the offending character with 'illegal input sequence at
position' message. Usually somewhere in the middle, so watch the
message line for 'iconv' string.

Eg:
badutf := []rune("abciconv: illegal input sequence at position
badutf := []rune("abc^-here

P.S. If you need line numbers you may annotate input with grep:
$ grep -n file.go | iconv -f utf7 2>&1 | tail -1



Hope this helps,

--
Wojciech S. Czarnecki
<< ^oo^ >> OHIR-RIPE

Taetso Madiba

unread,
Oct 12, 2021, 12:02:12 AM10/12/21
to golang-nuts
hey @Raphael,

did you ever find how to resolve this issue? I have a similar problem where I can compile on mac but not linux

Reply all
Reply to author
Forward
0 new messages