Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use golang on Raspberry Pi

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use golang on Raspberry Pi. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Pre-compilation preparation and configuration on Raspberry

one. As the saying goes, a skillful wife cannot make bricks without rice. To compile the source code, you have to download it and decompress it here to the / home/pi/go directory.

two. Confirm that the required tool systems are already in place: mercurial, gcc, and libc6-dev. If you are not sure, just enter the following command:

Sudo apt-get install-y mercurial gcc libc6-dev

The above command implements the installation of 3 necessary tools.

three. Configure environment variables. Because I unpacked the downloaded source code to the / home/pi/go directory at the first point, I set the value of the GOROOT variable to this path:

Export GOROOT=/home/pi/go

Export GOARCH=arm

But in fact, it is OK if it is not set, which defaults to the parent directory of the command line execution location.

four. It is also a configuration environment variable. The GOARM variable may not be set, and the default value is "6". However, it has been proved in practice that if it is not set to a value of "5", the following error will occur in compilation: SIGILL: illegal instruction. So what's the difference between 5 and 6? Setting GOARM to 5 causes the connector to issue instructions to invoke the software floating-point implementation instead of using hardware floating-point support.

Export GOARM= "5"

five. Allocate more memory for cpu use. This is not necessary, and more memory helps reduce compilation time. Use the memory_split function in sudo raspi-config to achieve this.

Compiling golang on Raspberry

Open the command line and go to the src directory under the go directory:

Cd / home/pi/go/src

Run the all.bash script

. / all.bash

Or if you want to see how much time it takes to compile, you can do this:

Time. / all.bash

Okay. After a long time:

Pi@raspberrypi ~ / go/src $time. / all.bash

Real 47m34.894s

User 44m23.260s

Sys 2m6.690s

The whole process took 47 minutes. The compilation is complete. On my computer, the process only takes 3 minutes.

Summary

The above procedures are just compiling golang compilers on Rapsberry. If we only want to run programs written in go on Raspberry, then we don't have to do this. We can use cross-compilation to complete the compilation of the code on our computers and then copy to run on Raspberry.

How to access the official website of golang

Recently, it has been found that the official website of the go language is no longer accessible, which brings a lot of inconvenience to learning. A relatively simple method is provided here.

one. Enter the command: nslookup www.google.com 8.8.8.8

Get a list of google's ip addresses, then randomly copy an ip and enter it on the browser to see if you can open the google search page. If you can, proceed to the second step.

two. Open the system hosts file and default to / etc/hosts under ubuntu12. Add a line:

Golang.org

For example: 74.125.128.102 golang.org

All right, let's see if we can open the official website!

Compile go programs on ubuntun and run on Raspberry

It's actually cross-compilation. Extract the golang source code to the / home/lixin/soft/goarm5 directory.

The command line goes to / home/lixin/soft/goarm5/src.

Run the following command:

Export GOROOT=/home/lixin/soft/goarm5

Export GOARCH=arm

Export GOARM=5

. / all.bash

In about a few minutes, a compiler has been built to run on Raspberry.

Then there are some hello world test programs.

Vim / home/lixin/main.go

The main.go file is as follows:

Package main

Import "fmt"

Func main () {

Fmt.Println ("hello lixin")

}

Cross-compilation:

/ home/lixin/soft/goarm5/bin/go build. / main.go

Copy the generated main file to run on Raspberry. Very good, correctly print out "hello lixin" on the screen

This is the end of the article on "how to use golang on Raspberry Pi". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report