7days-golang
7 days golang programs from scratch (web framework Gee, distributed cache GeeCache, object relational mapping ORM framework GeeORM, rpc framework GeeRPC etc) 7天用Go动手写/从零实现系列
Top Related Projects
A standard library for microservices.
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
⚡️ Express inspired web framework written in Go
High performance, minimalist Go web framework
Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
A curated list of awesome Go frameworks, libraries and software
Quick Overview
7days-golang is an educational project that guides developers through building various Go applications in 7 days. It covers topics like building a web framework, distributed cache, RPC framework, and more, providing hands-on experience with Go programming concepts.
Pros
- Offers practical, project-based learning for Go developers
- Covers a wide range of Go applications and concepts
- Provides step-by-step tutorials with explanations
- Suitable for both beginners and intermediate Go programmers
Cons
- May not cover the most advanced Go topics
- Some projects might be simplified for educational purposes
- Limited to 7 days of content, which may not be enough for in-depth learning
- Might not always reflect the latest Go best practices or features
Code Examples
Here are a few code examples from different projects in the repository:
- Web Framework (Day 1) - Defining a simple route handler:
func main() {
r := gee.New()
r.GET("/", func(c *gee.Context) {
c.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
})
r.Run(":9999")
}
- Distributed Cache (Day 4) - Creating a group cache:
func createGroup() *geecache.Group {
return geecache.NewGroup("scores", 2<<10, geecache.GetterFunc(
func(key string) ([]byte, error) {
log.Println("[SlowDB] search key", key)
if v, ok := db[key]; ok {
return []byte(v), nil
}
return nil, fmt.Errorf("%s not exist", key)
}))
}
- RPC Framework (Day 7) - Defining an RPC service:
type Foo int
type Args struct{ Num1, Num2 int }
func (f Foo) Sum(args Args, reply *int) error {
*reply = args.Num1 + args.Num2
return nil
}
Getting Started
To get started with 7days-golang:
-
Clone the repository:
git clone https://github.com/geektutu/7days-golang.git
-
Navigate to the desired project directory:
cd 7days-golang/gee-web
-
Run the example code:
go run main.go
-
Follow the tutorial for each day's project in the respective README files.
Competitor Comparisons
A standard library for microservices.
Pros of kit
- Comprehensive microservices toolkit with extensive features
- Well-established project with a large community and ecosystem
- Provides a standardized approach to building microservices
Cons of kit
- Steeper learning curve due to its complexity
- May be overkill for smaller projects or simple applications
- Requires more boilerplate code compared to lightweight alternatives
Code Comparison
kit example:
import (
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/transport/http"
)
func makeUppercaseEndpoint(svc StringService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(uppercaseRequest)
v, err := svc.Uppercase(req.S)
return uppercaseResponse{v, err}, nil
}
}
7days-golang example:
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/hello", helloHandler)
log.Fatal(http.ListenAndServe(":3000", nil))
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "This is the index page")
}
The kit example demonstrates its more structured approach with separate endpoint and transport layers, while 7days-golang showcases a simpler, more straightforward implementation using standard Go HTTP handlers.
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Pros of Gin
- Production-ready, full-featured web framework
- Extensive documentation and large community support
- High performance with minimal memory footprint
Cons of Gin
- Steeper learning curve for beginners
- More complex setup for simple projects
- Larger codebase and dependencies
Code Comparison
Gin example:
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
r.Run()
7days-golang example:
engine := gee.New()
engine.GET("/", func(c *gee.Context) {
c.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
})
engine.Run(":9999")
Key Differences
- 7days-golang is a learning project, while Gin is a production-ready framework
- Gin offers more features and middleware out of the box
- 7days-golang provides a step-by-step guide to building a web framework
- Gin has a larger ecosystem and third-party plugin support
Use Cases
- Choose Gin for production applications requiring robustness and performance
- Opt for 7days-golang to learn web framework internals and Go programming concepts
Community and Support
- Gin has a large, active community with frequent updates
- 7days-golang is primarily an educational resource with limited ongoing development
⚡️ Express inspired web framework written in Go
Pros of Fiber
- Production-ready, full-featured web framework
- High performance with low memory footprint
- Extensive middleware ecosystem and third-party integrations
Cons of Fiber
- Steeper learning curve for beginners
- Less focus on educational aspects and step-by-step learning
- May be overkill for simple projects or learning basic Go concepts
Code Comparison
7days-golang (simple HTTP server):
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
})
http.ListenAndServe(":8080", nil)
Fiber (equivalent HTTP server):
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":8080")
Summary
7days-golang is an educational project designed to teach Go basics through practical examples over seven days. It's ideal for beginners and those looking to understand Go fundamentals.
Fiber is a full-fledged web framework inspired by Express, offering high performance and a rich feature set. It's better suited for production applications and developers familiar with Go seeking a powerful web framework.
Choose 7days-golang for learning Go basics and simple projects, or Fiber for building robust, high-performance web applications with extensive features and middleware support.
High performance, minimalist Go web framework
Pros of Echo
- More mature and feature-rich web framework with extensive middleware support
- Better performance and scalability for large-scale applications
- Active community and regular updates
Cons of Echo
- Steeper learning curve for beginners compared to 7days-golang
- More complex setup and configuration for simple projects
- Larger codebase and dependencies
Code Comparison
Echo:
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
7days-golang:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
})
http.ListenAndServe(":8080", nil)
Summary
Echo is a more robust and feature-rich web framework suitable for large-scale applications, while 7days-golang focuses on teaching Go basics through simple projects. Echo offers better performance and scalability but has a steeper learning curve. 7days-golang is more beginner-friendly and easier to set up for small projects. The code comparison shows that Echo requires more setup but provides a more structured approach to handling routes and requests.
Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
Pros of mux
- More mature and widely adopted in production environments
- Extensive routing capabilities with support for URL parameters, subrouters, and middleware
- Well-documented with a larger community for support
Cons of mux
- Steeper learning curve for beginners compared to 7days-golang's simpler approach
- Potentially overkill for small projects or those learning Go basics
- Less focus on teaching Go fundamentals compared to 7days-golang's tutorial-style approach
Code Comparison
mux example:
r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
http.Handle("/", r)
7days-golang example:
engine := gee.New()
engine.GET("/", func(c *gee.Context) {
c.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
})
engine.Run(":9999")
Summary
mux is a robust routing solution for Go web applications, offering advanced features and widespread adoption. 7days-golang, on the other hand, is designed as a learning project to help developers understand Go web development concepts through a series of tutorials. While mux is more suitable for production-ready applications, 7days-golang serves as an excellent educational resource for those new to Go web development.
A curated list of awesome Go frameworks, libraries and software
Pros of awesome-go
- Comprehensive collection of Go resources, libraries, and tools
- Regularly updated with community contributions
- Well-organized into categories for easy navigation
Cons of awesome-go
- Lacks hands-on tutorials or practical examples
- Can be overwhelming for beginners due to the sheer volume of information
Pros of 7days-golang
- Focused, structured learning path for Go beginners
- Practical projects with step-by-step explanations
- Covers key Go concepts through real-world applications
Cons of 7days-golang
- Limited in scope compared to the extensive resources in awesome-go
- May not cover advanced topics or specialized libraries
Code Comparison
7days-golang (Day 1 - HTTP Server):
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/hello", helloHandler)
log.Fatal(http.ListenAndServe(":3000", nil))
}
awesome-go (No direct code examples, but links to various HTTP server libraries):
// Example using the Gin framework
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
r.Run(":8080")
Note: The code comparison is limited as awesome-go primarily provides links to resources rather than direct code examples.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
7 days golang programs from scratch
README ä¸æçæ¬
7天ç¨Goä»é¶å®ç°ç³»å
7天è½åä»ä¹å¢ï¼ç±»ä¼¼ gin ç web æ¡æ¶ï¼ç±»ä¼¼ groupcache çåå¸å¼ç¼åï¼æè ä¸ä¸ªç®åç Python 解éå¨ï¼å¸æè¿ä¸ªä»åºè½ç»ä½ çæ¡ã
æ¨èå é 读 **Go è¯è¨ç®ææç¨**ï¼ä¸ç¯æç« äºè§£Goçåºæ¬è¯æ³ã并åç¼ç¨ï¼ä¾èµç®¡ççå 容ã
æ¨è **Go è¯è¨ç¬è¯é¢è¯é¢**ï¼å 深对 Go è¯è¨çç解ã
æ¨è Go è¯è¨é«æ§è½ç¼ç¨(项ç®å°å)ï¼ååºé«æ§è½ç Go 代ç ã
æå¾ å ³æ³¨æçãç¥ä¹ä¸æ ãåãå¾®åãï¼æ¥çæè¿çæç« åå¨æã
7天ç¨Goä»é¶å®ç°Webæ¡æ¶ - Gee
Gee æ¯ä¸ä¸ªæ¨¡ä»¿ gin å®ç°ç Web æ¡æ¶ï¼Go Ginç®ææç¨å¯ä»¥å¿«éå ¥é¨ã
- 第ä¸å¤©ï¼åç½®ç¥è¯(http.Handleræ¥å£) | Code
- 第äºå¤©ï¼ä¸ä¸æ设计(Context) | Code
- 第ä¸å¤©ï¼Trieæ è·¯ç±(Router) | Code
- 第å天ï¼åç»æ§å¶(Group) | Code
- 第äºå¤©ï¼ä¸é´ä»¶(Middleware) | Code
- 第å 天ï¼HTML模æ¿(Template) | Code
- 第ä¸å¤©ï¼é误æ¢å¤(Panic Recover) | Code
7天ç¨Goä»é¶å®ç°åå¸å¼ç¼å GeeCache
GeeCache æ¯ä¸ä¸ªæ¨¡ä»¿ groupcache å®ç°çåå¸å¼ç¼åç³»ç»
- 第ä¸å¤©ï¼LRU ç¼åæ·æ±°çç¥ | Code
- 第äºå¤©ï¼åæºå¹¶åç¼å | Code
- 第ä¸å¤©ï¼HTTP æå¡ç«¯ | Code
- 第å天ï¼ä¸è´æ§åå¸(Hash) | Code
- 第äºå¤©ï¼åå¸å¼èç¹ | Code
- 第å 天ï¼é²æ¢ç¼åå»ç©¿ | Code
- 第ä¸å¤©ï¼ä½¿ç¨ Protobuf éä¿¡ | Code
7天ç¨Goä»é¶å®ç°ORMæ¡æ¶ GeeORM
GeeORM æ¯ä¸ä¸ªæ¨¡ä»¿ gorm å xorm ç ORM æ¡æ¶
gorm åå¤æ¨åºå®å ¨éåç v2 çæ¬(ç®åè¿å¨å¼åä¸)ï¼ç¸å¯¹ gorm-v1 æ¥è¯´ï¼xorm ç设计æ´å®¹æç解ï¼æ以 geeorm æ¥å£è®¾è®¡ä¸ä¸»è¦åèäº xormï¼ä¸äºç»èå®ç°ä¸åèäº gormã
- 第ä¸å¤©ï¼database/sql åºç¡ | Code
- 第äºå¤©ï¼å¯¹è±¡è¡¨ç»ææ å° | Code
- 第ä¸å¤©ï¼è®°å½æ°å¢åæ¥è¯¢ | Code
- 第å天ï¼é¾å¼æä½ä¸æ´æ°å é¤ | Code
- 第äºå¤©ï¼å®ç°é©å(Hooks) | Code
- 第å 天ï¼æ¯æäºå¡(Transaction) | Code
- 第ä¸å¤©ï¼æ°æ®åºè¿ç§»(Migrate) | Code
7天ç¨Goä»é¶å®ç°RPCæ¡æ¶ GeeRPC
GeeRPC æ¯ä¸ä¸ªåºäº net/rpc å¼åç RPC æ¡æ¶
GeeRPC æ¯åºäº Go è¯è¨æ ååº net/rpc
å®ç°çï¼æ·»å äºå议交æ¢ãæå¡æ³¨åä¸åç°ãè´è½½åè¡¡çåè½ï¼ä»£ç 约 1kã
- 第ä¸å¤© - æå¡ç«¯ä¸æ¶æ¯ç¼ç | Code
- 第äºå¤© - æ¯æ并åä¸å¼æ¥ç客æ·ç«¯ | Code
- 第ä¸å¤© - æå¡æ³¨å(service register) | Code
- 第å天 - è¶ æ¶å¤ç(timeout) | Code
- 第äºå¤© - æ¯æHTTPåè®® | Code
- 第å 天 - è´è½½åè¡¡(load balance) | Code
- 第ä¸å¤© - æå¡åç°ä¸æ³¨åä¸å¿(registry) | Code
WebAssembly 使ç¨ç¤ºä¾
å ·ä½çå®è·µè¿ç¨è®°å½å¨ Go WebAssembly ç®ææç¨ã
What can be accomplished in 7 days? A gin-like web framework? A distributed cache like groupcache? Or a simple Python interpreter? Hope this repo can give you the answer.
Web Framework - Gee
- Day 1 - http.Handler Interface Basic Code
- Day 2 - Design a Flexiable Context Code
- Day 3 - Router with Trie-Tree Algorithm Code
- Day 4 - Group Control Code
- Day 5 - Middleware Mechanism Code
- Day 6 - Embeded Template Support Code
- Day 7 - Panic Recover & Make it Robust Code
Distributed Cache - GeeCache
GeeCache is a groupcache-like distributed cache
- Day 1 - LRU (Least Recently Used) Caching Strategy Code
- Day 2 - Single Machine Concurrent Cache Code
- Day 3 - Launch a HTTP Server Code
- Day 4 - Consistent Hash Algorithm Code
- Day 5 - Communication between Distributed Nodes Code
- Day 6 - Cache Breakdown & Single Flight | Code
- Day 7 - Use Protobuf as RPC Data Exchange Type | Code
Object Relational Mapping - GeeORM
GeeORM is a gorm-like and xorm-like object relational mapping library
Xorm's desgin is easier to understand than gorm-v1, so the main designs references xorm and some detailed implementions references gorm-v1.
- Day 1 - database/sql Basic | Code
- Day 2 - Object Schame Mapping | Code
- Day 3 - Insert and Query | Code
- Day 4 - Chain, Delete and Update | Code
- Day 5 - Support Hooks | Code
- Day 6 - Support Transaction | Code
- Day 7 - Migrate Database | Code
RPC Framework - GeeRPC
GeeRPC is a net/rpc-like RPC framework
Based on golang standard library net/rpc
, GeeRPC implements more features. eg, protocol exchange, service registration and discovery, load balance, etc.
- Day 1 - Server Message Codec | Code
- Day 2 - Concurrent Client | Code
- Day 3 - Service Register | Code
- Day 4 - Timeout Processing | Code
- Day 5 - Support HTTP Protocol | Code
- Day 6 - Load Balance | Code
- Day 7 - Discovery and Registry | Code
Golang WebAssembly Demo
Top Related Projects
A standard library for microservices.
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
⚡️ Express inspired web framework written in Go
High performance, minimalist Go web framework
Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
A curated list of awesome Go frameworks, libraries and software
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot