This is a simple URL shortener project built with Go. It’s a great beginner project to get hands-on with Go. You’ll learn how to create a basic web server, use goroutines to run tasks in the background, and handle data with maps.
- Go Modules: How to set up and manage your Go project.
- HTTP Servers: How to build a web server with Go.
- Goroutines: Running the server and CLI in parallel.
- Maps and Concurrency: Storing and accessing data safely.
You only need one file for this project:
GoShorten-CLI/
│
├── main.go
├── go.mod
└── README.md
This file contains all the code to handle URL shortening and redirection.
package main
import (
"fmt"
"net/http"
"strings"
"sync"
)
// URLStore is a map that links short URLs to long URLs.
var (
URLStore = make(map[string]string)
mu sync.Mutex
)
// RedirectHandler redirects short URLs to the long URL.
func RedirectHandler(w http.ResponseWriter, r *http.Request) {
shortURL := r.URL.Path[1:] // Get short URL from the request
mu.Lock()
longURL, exists := URLStore[shortURL]
mu.Unlock()
if exists {
http.Redirect(w, r, longURL, http.StatusFound)
} else {
http.NotFound(w, r) // Show 404 if the short URL isn’t found
}
}
func main() {
// Start the HTTP server in a separate goroutine.
go func() {
http.HandleFunc("/", RedirectHandler)
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Printf("Server failed to start: %v\n", err)
}
}()
fmt.Println("Server running at http://localhost:8080")
for {
var longURL, shortURL string
// Get the long URL from the user.
fmt.Print("Enter long URL: ")
fmt.Scanln(&longURL)
// Add "https://" if the URL doesn’t start with it.
if !strings.HasPrefix(longURL, "http://") && !strings.HasPrefix(longURL, "https://") {
longURL = "https://" + longURL
}
// Get a custom short URL or generate one if none is provided.
fmt.Print("Enter custom short URL (optional): ")
fmt.Scanln(&shortURL)
if shortURL == "" {
shortURL = fmt.Sprintf("%d", len(URLStore)+1)
}
// Save the mapping from short URL to long URL.
mu.Lock()
URLStore[shortURL] = longURL
mu.Unlock()
fmt.Printf("Short URL: http://localhost:8080/%s\n\n", shortURL)
}
}
- Imports: Includes libraries for HTTP, strings, and concurrency.
- Global Variables:
URLStore
: Keeps track of short URLs and their long counterparts.mu
: A mutex to ensure thread safety when multiple users are interacting with the URL shortener.
- RedirectHandler: Handles redirection from short URLs to long URLs.
- Main Function:
- Starts the HTTP server in the background.
- Continuously prompts for URLs and handles user input.
- Stores the URLs and prints out the shortened URL.
- Go 1.16 or later installed on your computer.
-
Clone the Repo:
git clone https://github.com/hemanth0525/GoShorten-CLI.git
-
Set Up Go Modules:
go mod init GoShorten-CLI go mod tidy
-
Run the Code:
go run main.go
or (if you have
go.mod
in your project):go run .
- Enter a long URL when prompted (e.g.,
google.com
). - Provide a custom short URL or just press Enter to let the program create one for you.
- The program will give you a short URL that you can use to redirect to the original URL.
Enter long URL:
google.com
Enter custom short URL (optional):
goog
Short URL: http://localhost:8080/goog
Visit http://localhost:8080/goog
in your browser to go to https://google.com
.
This project is licensed under the MIT License. Check out the LICENSE file for details.