If you see this error as you’re working on this tutorial, make sure you’ve stopped your program from a previous step before running it again. Sometimes this can happen if the port is commonly used and another program on your computer is using it, but it can also happen if you run multiple copies of your own program multiple times. This error can be returned when ListenAndServe is unable to listen on the address or port you’ve provided because another program is already using it. One error you may see while running your program is the address already in use error. If this happens, it will print the error to the screen and then exit the program with an error code of 1 using the os.Exit function. In the second error check, you check for any other error. This is usually an expected error because you’ll be shutting down the server yourself, but it can also be used to show why the server stopped in the output. The first error you’re checking for, http.ErrServerClosed, is returned when the server is told to shut down or close. Import ( "errors" "fmt" "io" "net/http" "os" ) func getRoot (w http. Once you’ve set up your handlers, call the http.ListenAndServe function to start the server and listen for requests.Īdd the following code to the file to start your program and set up the handlers: Then, you’ll create a main function and use it to set up your request handlers with the http.HandleFunc function by passing it the / path for the getRoot handler function and the /hello path for the getHello handler function. In the main.go file, you will create two functions, getRoot and getHello, to act as your handler functions. Now, inside the httpserver directory you created, use nano, or your favorite editor, to open the main.go file: Then, you’ll use the http.ListenAndServe function to start the server and tell it to listen for new HTTP requests and then serve them using the handler functions you set up. In this section, you’ll start by using the function http.HandleFunc to tell the server which function to call to handle a request to the server. Listening for Requests and Serving ResponsesĪ Go HTTP server includes two major components: the server that listens for requests coming from HTTP clients and one or more request handlers that will respond to those requests. Now that you have your program’s directory created and you’re in the httpserver directory, you can start implementing your HTTP server. In this case, use the directory httpserver: Next, make the directory for your project and navigate to that directory. In this tutorial, you’ll use a directory named projects.įirst, make the projects directory and navigate to it: Many developers keep their projects in a directory to keep them organized. Then, you will expand that program to run multiple HTTP servers in the same program.īefore you write any code, though, you’ll need to get your program’s directory created. In this section, you will create a program that uses the http.ListenAndServe function to start an HTTP server that responds to the request paths / and /hello. The net/http package not only includes the ability to make HTTP requests, but also provides an HTTP server you can use to handle those requests. In Go, most of the HTTP functionality is provided by the net/http package in the standard library, while the rest of the network communication is provided by the net package. Familiarity with how HTTP requests are composed and sent (recommended).Experience running goroutines and reading channels, which can be gained from the tutorial, How To Run Multiple Functions Concurrently in Go.Experience with Go’s context package, which can be attained in the tutorial, How To Use Contexts in Go.Familiarity with using JSON in Go, which can be found in the How To Use JSON in Go tutorial.To read up on curl, check out How To Download Files with cURL. Ability to use curl to make web requests.To set this up, follow the How To Install Go tutorial for your operating system. You’ll also update your program to respond to the request with your own HTTP headers and status codes. In this tutorial, you will create an HTTP server using Go’s standard library and then expand your server to read data from the request’s query string, the body, and form data. The Go standard library provides built-in support for creating an HTTP server to serve your web content or making HTTP requests to those servers. The Hypertext Transfer Protocol (HTTP) serves much of this content, whether it’s a request for a cat image or a request to load the tutorial you’re reading now. Many developers spend at least some of their time creating servers to distribute content on the internet. The author selected the Diversity in Tech Fund to receive a donation as part of the Write for DOnations program.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |