Site icon Karneliuk

From Python To Go 001. Get Started.

Dear friend,

As mentioned in previous blogpost, I’ve kicked the new series of blog posts related to Go (Golang programming language) and how to pick that up. Originally my idea was just to explain some concepts, pretty much I’ve done back in past with Code eXpress (CEX) for Python. But then I’ve thought through it further and decided to write a side-by-side guide with Python and Go together, exactly as I’ve done before with multi vendor network automation, when started writing about Nokia SR OS and Cisco IOS XR back in 2016.

Do I Need Both Python And Go?

In our opinion, yes, you do need both. Each of these programming languages shines in some areas more than another. And both of them are applicable to network and infrastructure automation. As such, we recommend to study both, but to start with Python as it is easier and at this stage is wider used than Go. So we encourage you to start with our Network Automation Trainings:

We offer the following training programs in network automation for you:

During these trainings you will learn the following topics:

Moreover, we put all mentions technologies in the context of real use cases, which our team has solved and are solving in various projects in the service providers, enterprise and data centre networks and systems across the Europe and USA. That gives you opportunity to ask questions to understand the solutions in-depth and have discussions about your own projects. And on top of that, each technology is provided with online demos and labs to master your skills thoroughly. Such a mixture creates a unique learning environment, which all students value so much. Join us and unleash your potential.

Start your automation training today.

What Are We Going To Talk Today About?

Whenever you start dealing with a new programming language, the first thing is to figure out how to get it to you host (laptop, desktop, virtual machine, bare metal server, whatever you use). And right after it is installed, you typically want to test if that works. That’s what we are going to talk today about. To put it formally:

  1. How to install programming language (Python and Go) to your host?
  2. How to test that programming language works correctly?

Installing Programming Language

Python

Let’s quickly recap what you typically do get the Python. Basically, you have two options (we are talking about Linux and MAC OS, sorry Windows users):

  1. You install it using the package manager (e.g.,
    1
    apt-get
    in Ubuntu/Debian,
    1
    dnf
    in Red Hat Enterprise Linux / Fedora and alike, or
    1
    homebrew
    in MAC OS). There maybe some additional steps, such as adding repositories.
  2. You build it from source locally on your host.

Despite the latter approach is more involved, it allows you to select the specified version of Python as well as to have multiple versions of Python working in parallel on the same host, which is quite useful when you develop and support Python code, but when you cannot normalize version of Python itself. I had a real case, where the default version of Python used in the company was different to version of Python needed by certain external packages.

Back in the days we’ve created a detailed guide to how you can get Python installed via local build. This guide is still relevant, so instead of repeating it, we suggest you just read that blog post from our CEX series. All the Python versions you can get from the official website.

Once the installation is completed, check that you can use Python:


1
2
$ python3.10 -V
Python 3.10.15

Go (Golang)

Apparently installation process of Go from the user perspective is simpler according to the official documentation.

First of all, you need to download the archive or installer from the Go download index.

Make sure you select the architecture right: if you install on Macbooks with M chips or on Raspberry PI or alike, you shall select arm64 instead of amd64.

Here is an example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ wget https://go.dev/dl/go1.23.2.linux-amd64.tar.gz
--2024-11-03 10:18:48--  https://go.dev/dl/go1.23.2.linux-amd64.tar.gz
Resolving go.dev (go.dev)... 2001:4860:4802:34::15, 2001:4860:4802:36::15, 2001:4860:4802:38::15, ...
Connecting to go.dev (go.dev)|2001:4860:4802:34::15|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.google.com/go/go1.23.2.linux-amd64.tar.gz [following]
--2024-11-03 10:18:49--  https://dl.google.com/go/go1.23.2.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 2a00:1450:4009:815::200e, 142.250.178.14
Connecting to dl.google.com (dl.google.com)|2a00:1450:4009:815::200e|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 73611540 (70M) [application/x-gzip]
Saving to: ‘go1.23.2.linux-amd64.tar.gz’

go1.23.2.linux-amd64.tar.gz                        100%[===============================================================================================================>]  70.20M  9.27MB/s    in 8.1s    

2024-11-03 10:18:57 (8.72 MB/s) - ‘go1.23.2.linux-amd64.tar.gz’ saved [73611540/73611540]

It is heartwarming to see IPv6 being used by default

Next step is to remove the existing Go installation (if any) and install the new version of Go (Golang):


1
$ sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.23.2.linux-amd64.tar.gz

You shall not get back any response and in a few moments (on Raspberry PI4 it was quite a few moments) you will get back your prompt.

Final step is to add location with Go to your

1
PATH
in environment:

1
2
$ echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
$ source ~/.bashrc

Once these operations are completed, you shall be able to run the command displaying you version of Go:


1
2
$ go version
go version go1.23.2 linux/amd64

Installation of both Go and Python is completed, so let’s create a basic app to verify languages work properly.

Hello, World!

Typically this is the first program to be written in any network language, as it gives some taste of programming language syntax and how to interact with it. All the program is to do is to simply print to console (what is formally called stdout – standard output) some phrase. As we are focusing on network automation, we will print phrase “hello network automation”.

Python

As this is very simple operation, we’ll add also exposure to comments in code. Here is Python example:


1
2
3
4
5
$ cat main.py
"""From Python to Go: Python: 001 - Hello, World!"""

# That's it! Just print the message.
print("Hello, network automation!")

The actual code, which does the job is only line

1
print()
, which is a function, which takes argument
1
"Hello, network automation!"
and prints it to stdout.

What you also have here are comments to code:

  1. Multi-line comment framed by triple quote symbols
    1
    """ text """
    , which is called docustring . In this example it is only one line, but it can be more. It is also used by developers environment (e.g. VSCode).
  2. Single line comment starting with character hash and followed by some text (e.g.,
    1
    # something
    ), which is typically used as note for self or other developers about what’s going in code.

As Python is interpreted programming language, all you need is to specify path to interpreter and to file with Python code for the application to run:


1
2
$ python3 main.py
Hello, network automation!

Alright, Python works so we can try the same in Go.

Go (Golang)

Let’s repeat the very same logic in Go: printing to console and putting some comments in code:


1
2
3
4
5
6
7
8
9
10
11
12
$ cat main.go
/* From Python to Go: Go: 001 - Hello, World! */
package main

import (
    "fmt"
)

func main() {
    // That's it just print the message
    fmt.Println("Hello, network automation!")
}

We have a bit more lines of code, no wonder as Go is very different programming language. The key components:

  1. 1
    package main
    is an important statement in the Go code, as it attributes the files (as it could be more files for a single app) to the application itself. All the files sharing the same
    1
    package main
    is what typically used when this application run.
  2. Within
    1
    package main
    we also shall have main function defined as
    1
    func main() {}
    with the execution code of the core application logic residing within the curly braces.
  3. Then, to get access to functionality to print to stdout, we need to
    1
    import
    package fmt. Import exists in Python and does the same logic, i.e. it allows you reference external packages in your code to leverage already created functionality. We don’t need it in our basic example, though. So we will cover that in future posts.
  4. The package fmt contains function
    1
    Println()
    , which has the same functionality as in Python: it takes one or more arguments and print them to stdout.
  5. Multi-line comments in Go are framed within slashes with asterisks:
    1
    /* comment */
    .
  6. Single-line comments in Go are starting with double slash:
    1
    // comment
    .

Go is a compiled programming language, meaning we need first to compile the code code before we can execute it. The benefit of compiled languages is a possibility to build an app in a binary format on host running the programming language and then to distribute the created binary image to all other hosts, which don’t necessary have programming language installed. To build the app, we need to do these two steps within the directory with our

1
main.go
code:

1
2
3
4
5
6
$ go mod init karneliuk.com/go/001
go: creating new go.mod: module karneliuk.com/go/001
go: to add module requirements and sums:
        go mod tidy

$ go build .

The first command file creates a module description, with a name of the module, version of Go used during the build. This is important file, which we will discuss more later, when we talk about

1
import
in Go and Python in-depth.

The second command builds a binary using all the files (typically all files with

1
.go
extension) within the build directory. Once the build is completed, you will see a new file within the directory:

1
2
3
4
5
6
7
$ ls -larth
total 2.1M
drwxrwxr-x 4 anton anton 4.0K Nov  3 12:56 ..
-rw-rw-r-- 1 anton anton   39 Nov  3 13:00 go.mod
-rw-rw-r-- 1 anton anton  178 Nov  3 16:03 main.go
-rwxrwxr-x 1 anton anton 2.1M Nov  3 16:13 001
drwxrwxr-x 2 anton anton 4.0K Nov  3 16:13 .

Now you can execute it:


1
2
$ ./001
Hello, network automation!

As build process is very often during the development, there is a shortcut in

1
Go
language, which allows you to build temporary file in
1
/tmp
directory and execute build file immediately followed by automated deletion of created file:

1
2
$ go run .
Hello, network automation!

Lessons in GitHub

You can find the final working versions of the files from this blog at out GitHub page.

Conclusion

We are yet to find a balance between what we put in the blog and what we don’t. There are a lot of great tutorials, books, videos, etc out there. What is our goal here is document our own learning curve and to help you with yours. We are looking forward to hear your feedback on what you like and what you don’t in our blogposts, so that we can calibrate.

Support us






P.S.

If you have further questions or you need help with your networks, I’m happy to assist you, just send me message. Also don’t forget to share the article on your social media, if you like it.

BR,

Anton Karneliuk 

Exit mobile version