Techlogs
Published in

Techlogs

Learning Go — Hello World

This is outdated; Use Go modules now. No hell with GOPATH

Complete example here https://github.com/alexcpn/golang_grpc_example

I will try to explain this later. But there is a trick here

replace interfaces => ./generatedreplace server => ./server

— — — — — — — — — — — — -Outdated below — — — — — — —

Step 1: Set Up Your Environment or Don’t

What does a developer spend the most time when trying to learn new technology?

Let us skip the setting up part and use Docker

Actually setting up this in your machine is also not a pain, But I get my GOPATH mixed up and there are too many workspaces. So, for now, I find it sane to work inside Docker.

And use Docker volume mapping to work on the code on the host and get it reflected in the container (inside the running docker)

I work on a Linux machine- Docker now runs also on Windows 10 .

My Workspace in host is /home/alex/coding, and I am behind a proxy — if you are not, then no need to add the http_proxy parts.

Once we are in the container. Let us create a simple Hello World first

Create a directory called go_learn in your host and in the container let us navigate to it

Let us follow a convention — All editing in host, all compilation in container

I use Visual Studo Code as an IDE .It is free , it is cool and you can use it for multiple languages too.

If you have not used VS Code this may be a good time to try it out. It has Go support by Microsoft itself

THE DAMNED GOPATH

Let us create our sweet and simple Hello World — Create a folder hello under go_learn folder and in it create hello.go

/coding/go_learn/src/hello/hello.go

Let us compile it and build an executable

can't load package: package hello: cannot find package "hello" in any of:
/usr/local/go/src/hello (from $GOROOT)
/go/src/hello (from $GOPATH)

Go Lang Package Structure

These are the rules and you need to follow it to make your coding life easier

All code in Go is organised under packages under a src folder

A package is a folder

The name of the package is the name of the folder (we will see more)

Except for package main — which if you put in a folder, make sure there is only one file in that

Take a look a hello.go again. It has a package main is in hello folder

And Go is complaining it cannot find it. It looks at the GOPATH env variable

The default GOPATH is /go/src

Let us reset it to our folder

export GOPATH=/coding/Prototypes/go_learn

and now again try to install it

Which basically compiles and builds it. There is a difference between install and build, we will check that later

You will see the executable in bin folder. Lets run it

We have crossed now the hardest barrier to learning GO- GOPATH :). We will encounter this more when we install extra packages and use Go packaging tools.

Also, we learned about packages. Next Episode we do a lot more packages and check out Interfaces

Part 2 of this series: Hello Interface

https://medium.com/techlogs/learning-go-hello-interface-7074df6752be

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store