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 build Golang Development Environment in Docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to build the Golang development environment in Docker. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Get Docker image

We use the latest stable version of the official image golang:latest. Before obtaining the image, you have to make sure that the Docker client is running on your computer. If not, go to the official website and download it.

Download the image on dockerHub and use the command directly:

Docker pull golang

After downloading the image, run a container with the image:

Docker run-rm-it-name go-http-demo golang bash

The above command creates a container called go-http-demo with the mirror golang, and creates a Bash session in the container. The-- rm option specifies that the container is automatically removed when it exits.

After running the above command, we enter the running container and run go version to view the following go version:

[email protected]: / go# go version

Go version go1.13.6 linux/amd64

[email protected]: / go#

You can find the version of the golang image running container in https://hub.docker.com/_/golang according to your needs.

Run the Go project in the container

We use the source code of HTTPServer that supports graceful shutdown service written at the end of "learn more about writing a HTTP server in Go" and put it in a Docker container to serve host requests. The source code of HTTP Server is as follows. If you want to explore how the code is realized, just look at the article in the link.

Package mainimport ("context"fmt"log"net/http"os"os/signal"syscall") func main () {mux: = http.NewServeMux () mux.Handle ("/", & helloHandler {}) server: = & http.Server {Addr: ": 8080", Handler: mux,} / / create the system signal receiver done: = make (chan os.Signal) signal.Notify (done, os.Interrupt, syscall.SIGINT) Syscall.SIGTERM) go func () {

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

Database

Wechat

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

12
Report