Golang 2 lines of code running an HTTP Web service on an arm Linux-based Raspberry Pi, Orangepi

Source: Internet
Author: User
Tags visual compiler

The Go Language (Golang) simplifies cross-platform cross-compilation steps, enabling cross-compiling of Arm+linux platform-based applications under Windows systems without the need for additional dependent libraries at runtime. The following is a simple HTTP server for example, the first source:

************************************************************

Package Main

Import (

"Net/http"

)

funcMain () {

http. Handle ("/", HTTP. Fileserver (http. Dir ("HTML")))

http. Listenandserve ("0.0.0.0:8080", nil)

}

************************************************************

The code in the main function shows:

First line: Set the home directory as the HTML directory in the startup directory

Second line: Start the HTTP service, listen on port 8080

************************************************************

Step 1: Install the Go Language development environment and Visual compiler Liteide under the Windows system (I'm using WIN10) (the steps are simple, many online tutorials)

Step 2: Create a new Go1 Command Project, named "Test"

Step 3:import is modified to "Net/http", the main () function is modified to the following two lines of code:

http. Handle ("/", HTTP. Fileserver (http. Dir ("HTML")))

http. Listenandserve ("0.0.0.0:8080", nil)

Step 4: The target environment is modified to CROSS-ARM6, according to "Ctrl+b" compiled, the generated execution file is the source directory "Test", no suffix

Step 5: Copy "Test" to your arm Development Board (Raspberry Pi Raspberrypi, Orange pie Orangepi), and authorize: sudo chmod 777 test, while creating a new "HTML" directory in the "test" directory, run./test

Next, you can access the HTTP service via/HTTP Development Board ip:8080, remember to create a new "HTML" directory, put in some HTML files/other files, you can browse and download like a regular website.

************************************************************

1. This example creates a static Web site that adds HTTP to the main function. Handlefunc can increase dynamic processing power

2. In Raspberry Pi 2b,orange Pi Lite test pass (ARM version forward compatible)

Golang 2 lines of code running an HTTP Web service on an arm Linux-based Raspberry Pi, Orangepi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.