Home > Backend Development > Golang > golang install net http

golang install net http

王林
Release: 2023-05-10 15:17:08
Original
555 people have browsed it

Golang is an open source programming language, and its emergence has caused great repercussions. Golang's fast compilation and execution speed and powerful performance make it the preferred language for web development and network programming. Among them, net/http is one of the most commonly used network programming libraries in Golang. Today, we will introduce how to install and use net/http in Golang.

1. Install Golang

First, we need to install Golang locally. Visit Golang's official website (https://golang.org/dl/) to download the installation package suitable for your operating system. After installation, we need to set environment variables.

2. Install the net/http package

Golang has its own package manager-go mod. We can use this manager to easily install the required packages. We only need to enter the following command on the command line:

go get -u net/http
Copy after login

This command will automatically download and install the net/http package to your local computer. If you need to use this package, you only need to import it in the code:

import "net/http"
Copy after login

3. Common usage

After installing the net/http package, we can start network programming. Here are some common usages.

  1. Start a simple HTTP server
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
        fmt.Fprintf(writer, "hello world")
    })
    fmt.Println("Server is listening at port 8080")
    http.ListenAndServe(":8080", nil)
}
Copy after login

The above code starts a simple HTTP server, which listens to port 8080. When we access http in the browser: //localhost:8080, the server will return the content of "hello world". Among them, the http.HandleFunc() function registers the processing function (the http.HandleFunc() function can also be used), and the http.ListenAndServe() function is used to start the HTTPServer instance.

  1. POST request
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
        if request.Method == "POST" {
            fmt.Fprint(writer, "POST method")
        } else {
            fmt.Fprint(writer, "GET method")
        }
    })
    fmt.Println("Server is listening at port 8080")
    http.ListenAndServe(":8080", nil)
}
Copy after login

In the above code, we determine whether the requested method is POST. If it is POST, return "POST method", otherwise return "GET method" ".

  1. File Service
package main

import (
    "net/http"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir("./")))
    http.ListenAndServe(":8080", nil)
}
Copy after login

The above code starts a file server and can access all files in the current directory. When we access http://localhost:8080/test.txt in the browser, the server will return the contents of the file.

4. Summary

This article briefly introduces how to install and use the net/http library in Golang. Although in actual use, there may be more complex situations. However, after mastering the basic usage, we can conduct in-depth study of network programming according to the actual situation and apply it to actual development.

The above is the detailed content of golang install net http. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template