Home >Backend Development >Golang >Go Ubuntu: Old School Style

Go Ubuntu: Old School Style

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-28 13:37:09860browse

Go   Ubuntu: Old School Style

If you're the type of person who likes cute interfaces and feature-packed tools, this article might not be for you. Here, let's do things the old-fashioned way, like our ancestors did. In the end, you will leave with Go installed and running, without the need for IDEs or polished interfaces (in case the frontend world ends for good).

What you will need:

  • Distributions based on Debian/Ubuntu, as the commands here are specific to this scenario;
  • Visual Studio Code installed (step-by-step link);
  • Sanity (optional, depending on the level of your GUI needs).

Installation

Open the terminal and paste the following command:

sudo apt install golang-go
  • sudo: is necessary to ensure that the command will be executed with administrator permissions (the famous "root");

  • apt: is the Ubuntu package management tool, used to install, update and remove software;

  • install: indicates that we are asking the system to install a new package;

  • golang-go: is the package you are installing, that is, Go (also known as Golang).

This command will install the version of Go present in the official Ubuntu repositories. The disadvantage is that this version is often not the most recent, as the repositories may not always be updated with the latest versions of all tools.


What if I want the most current version of Go?

To ensure you have the latest version of Go available, use the following commands:

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
sudo apt install golang-go
  • sudo add-apt-repository ppa:longsleep/golang-backports: is the command that will add a backports repository to your system. Backport repositories are like “alternative paths” to obtain newer versions of packages that are not in the official repository;

  • sudo apt update: will update the list of available packages and ensure that the system is aware of this new source of packages;

  • sudo apt install golang-go: ensures that you download the most current version of Go available in this backports repository.


Checking the installed version

After installation, check if Go was installed correctly by running the following command:

go version

This should return something like:

go version go1.23.4 linux/amd64

Now you're ready to take over the world with Go... or at least finish following the tutorial, if you don't mind.

It went wrong, what now?

Now turn around, right? The world will not dominate itself alone.


Programming in Go using VSCode

Now that Go is in the system, it's time to code using the VSCode terminal (I promise it doesn't hurt).

Open VSCode through the terminal with the command:

sudo apt install golang-go

Create a new project:

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
sudo apt install golang-go

If the following message in vbnet is as follows, then it means it worked.

go version

Now, run the ls command to list the files and create the directory structure:

go version go1.23.4 linux/amd64
  • mkdir: the command used to create a directory (a folder);

  • -p: option that allows you to create subdirectories, if they do not exist, without generating an error. For example, if you want to create a folder inside another, -p will create all the folders in the path at once;

  • project-name: is the name you choose for the directory that will be created, that is, we are creating the folder where we will store all the files of your Go project;

  • cd: means "change directory", changing directory. It is used to navigate between directories (folders) on your file system;

  • project-name: is the directory we just entered. So, after running this command, the terminal will be inside the project-name folder, where you can start working and adding files;

  • touch: command used to create an empty file. In this case, it creates the main.go file within the project-name directory (if the file already exists, the command updates the modification date and time);

  • main.go: Go file where we will write the code for our project;

  • code: command to open VSCode, if you have it installed and configured in your terminal;

  • main.go: file you just created, which will be opened directly in VSCode. This is useful to launch the file and start programming in Go directly from within the editor.

Now, finally inside main.go, paste the following code for a simple "Hello":

code .

Run via terminal:

go mod init nome-projeto

Ready! Now you will definitely dominate the world.


Want to be more old school? More coming!

Now, if you want to be more "thick", how about creating the file directly in the terminal without using a text editor?

Open the terminal in Ubuntu and create a file called hello.go with the Go code directly in it:

go: creating new go.mod: module projeto
go: to add module requirements and sums:
        go mod tidy
  • echo -e allows you to print multiple lines of text in the terminal (and -e interprets line breaks and other special characters);

  • The text between the quotes is the Go code we are inserting into the file;

  • > hello.go redirects the output of the echo command to the file hello.go.

Now, let's definitely raise the level.


Running...

sudo apt install golang-go

This command will compile and execute the Go file at the same time, resulting in:

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
sudo apt install golang-go

Modifying the code directly in the terminal

If you want to make small changes to the code directly in the terminal, you can use echo again. For example, if you want to change the message to "Hello, World!", run the command:

go version

Then run again:

go version go1.23.4 linux/amd64

Tip: using echo with redirection is a quick and effective way to create small files directly through the terminal. However, for larger projects, you will need a text editor like nano or vim in the terminal.


Conclusion

No more, except that now you are a true legend, and you will make this world a bluer place. GO!

The above is the detailed content of Go Ubuntu: Old School Style. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn