Hello World Barrier Help!

Hi guys,
Golang is my first Prog lang. and I can’t seem to break thru the Hello World barrier.
Here’s a quick rundown what I’ve done:
a)Install Go and place a Go workspace in my doc,
b)Set up bin, pkg and src folders but no files except hello world in bin folder
c)Run it in my windows CMD terminal all the way till 'go build helloworld.go’
Then this pops up:"can’t load package ,package main:helloworld.go:1:1: expected 'package, found 'EOF’
I do not know whether I miss anything along the way perhaps the importing of files into my workspace but I;m unsure.Any help will be appreciated:(

Hi,

I cannot tell for sure what’s wrong, but I would do these steps:

  1. Call

     go env GOPATH
    

and verify if the output is the GOPATH that you would expect.

  1. Place any .go file under $GOPATH/src. You can use a subdirectory here; as an example, let’s call it “helloworld”. If your .go file is called main.go, your Go workspace should now look like this (where GOPATH is the output from go env GOPATH:

     GOPATH
     +-- src
     |   +-- helloworld
     |       +-- main.go
     +-- pkg
     +-- bin
    

Now cd into GOPATH/src/helloworld and edit main.go.

A basic hello world code is:

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}

Save the file and run

go run main.go

and you should get a “Hello, world!” back. (No binary file is created in this case.)

Then run

go build main.go

and you should get a binary next to your source file. And if you run

go install main.go

you should find the binary in GOPATH/bin.

3 Likes

Hi Christopher,
Do you do it under the terminal and why the $ sign?

Sorry, my bad. $GOPATH is the Unix shell syntax for the value of environment variable GOPATH. If you use Windows, the syntax would be %GOPATH% instead. Here, I used the syntax only as a shortcut for “the directory that your GOPATH points to”. I realize now that this was too sloppy. Moreover, the $GOPATH environment variable may not even exist on your system. (It is optional since Go 1.8.) The command go env GOPATH is the authoritative way of determining the current value of the GOPATH.

And yes, the go commands require a terminal.

It seems that one needs to install files into the workspace from the terminal.
The problem with manual installation for rookies is how and where to install.Look at GoDoc tons of them out there
Tutorials are either scattered or outdated:(
I’ve just done it thru VS editor installation, I look at all the new files in my workspace;wow how am I going to do it manually without help?

You don’t have to install files into the workspace from the terminal per se, it’s just how Go structures it’s package management. Basically here’s the gist of the situation, you set up a src, bin, and pkg directory tree under your workspace.

Any source code you write, should be done in the src directory.

Go
  ------->src
          {Folder} *.go files and other source code for your app
  ------>pkg
          {Folder} you will never put anything here yourself, running  { go install } in a src folder directory from terminal will compile your code and install it in pkg folder to be used in any future projects as an include

  ------>bin
          {Folder} again you will never put anything in here yourself, { go install } command from your terminal inside a folder containing src code ( .go files) will build the binary for your app and place it in the bin directory of your GOPATH

If you want to run an app for testing without installing its data to pkg and/or bin you can use the go build command from within the src directory to generate the binary there. I hope this has cleared it up for you.

Sorry I’m a bit confused, is this not installing something from terminal into your folder which is in your workspace?

It is, but the problem of how and where to install is taken care of for you by the tools themselves. You just have to work with your source code, does that make sense or am I not understanding your argument?

Thanks for your patience, I’m very new in this even in Frontend:)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.