DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Introduction to Robot Operation System (ROS)
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Legacy Code Refactoring: Tips, Steps, and Best Practices
  • Health Apps and Data Privacy: Best Practices for Developers

Trending

  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Top Book Picks for Site Reliability Engineers
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Google Cloud Document AI Basics
  1. DZone
  2. Data Engineering
  3. Data
  4. A Quick Start Guide of HTTP Server Using Go

A Quick Start Guide of HTTP Server Using Go

In this article, take a look at a quick start guide of HTTP Server using Go.

By 
Amjad Hossain user avatar
Amjad Hossain
·
Apr. 29, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.9K Views

Join the DZone community and get the full member experience.

Join For Free

Why We Need to Know HTTP Request and Response

Data is being shared with many software that could be web applications or mobile applications to visualize and make an impact. HTTP is the most popular and robust way to expose the data to different kinds of applications. 

Install Go

In case you don't have Go installed in your local machine, download the binary release suitable for your system.

https://golang.org/dl/

After downloading the package, open and follow the instructions, then go to the distribution file, which should be installed to /usr/local/go.

Now set the PATH of Go distribution to make the Go command available.

Java
 




x


 
1
export PATH=$PATH:/usr/local/go/bin



Create HTTP Server

In Go, it's just like magic to start an HTTP server where you just need to import "net/http" package and define the HTTP listen to port and server. Paste the following code into your first server.go file and save it. 

Go
 




xxxxxxxxxx
1


 
1
package main
2

          
3
import (
4
    "net/http"
5
)
6

          
7
func main() {
8
    http.ListenAndServe(":8080", nil)
9
}



Navigate to your working server.go file directory and build and run your go sever $ go run server.go

Paste the following URL into your browser and hit enter.

Plain Text
 




xxxxxxxxxx
1


 
1
http://localhost:8080/



You should be able to see the following in your browser:

Java
 




xxxxxxxxxx
1


 
1
404 page not found



404 is expected here because we have not created the /(root) handler yet. The new version of your server.go is the following:

Go
 




x


 
1
package main
2

          
3
import (
4
    "fmt"
5
    "net/http"
6
)
7

          
8
func main() {
9
    const PORT = 8080
10
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
11
        fmt.Fprintf(w, "This is simple http request handler")
12
    })
13
    http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil)
14
}
15

          



Type the following URL on your browser 

Plain Text
 




xxxxxxxxxx
1


 
1
 http://localhost:8080/ 



This time you should be able to see the following response in your browser:

Java
 




xxxxxxxxxx
1


 
1
This is simple http request handler



Hope this helped!

Plain text mobile app Java (programming language) Data (computing) Distribution (differential geometry) Release (computing) IT

Opinions expressed by DZone contributors are their own.

Related

  • Introduction to Robot Operation System (ROS)
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Legacy Code Refactoring: Tips, Steps, and Best Practices
  • Health Apps and Data Privacy: Best Practices for Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!