Convert Figma logo to code with AI

geektutu logo7days-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动手写/从零实现系列

15,235
2,419
15,235
48

Top Related Projects

26,478

A standard library for microservices.

77,851

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.

33,019

⚡️ Express inspired web framework written in Go

29,410

High performance, minimalist Go web framework

20,665

Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍

128,386

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:

  1. 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")
}
  1. 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)
        }))
}
  1. 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:

  1. Clone the repository:

    git clone https://github.com/geektutu/7days-golang.git
    
  2. Navigate to the desired project directory:

    cd 7days-golang/gee-web
    
  3. Run the example code:

    go run main.go
    
  4. Follow the tutorial for each day's project in the respective README files.

Competitor Comparisons

26,478

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.

77,851

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
33,019

⚡️ 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.

29,410

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.

20,665

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.

128,386

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

7 days golang programs from scratch

CodeSize LICENSE

README 中文版本

7天用Go从零实现系列

7天能写什么呢?类似 gin 的 web 框架?类似 groupcache 的分布式缓存?或者一个简单的 Python 解释器?希望这个仓库能给你答案。

推荐先阅读 **Go 语言简明教程**,一篇文章了解Go的基本语法、并发编程,依赖管理等内容。

推荐 **Go 语言笔试面试题**,加深对 Go 语言的理解。

推荐 Go 语言高性能编程(项目地址),写出高性能的 Go 代码。

期待关注我的「知乎专栏」和「微博」,查看最近的文章和动态。

7天用Go从零实现Web框架 - Gee

Gee 是一个模仿 gin 实现的 Web 框架,Go Gin简明教程可以快速入门。

7天用Go从零实现分布式缓存 GeeCache

GeeCache 是一个模仿 groupcache 实现的分布式缓存系统

7天用Go从零实现ORM框架 GeeORM

GeeORM 是一个模仿 gorm 和 xorm 的 ORM 框架

gorm 准备推出完全重写的 v2 版本(目前还在开发中),相对 gorm-v1 来说,xorm 的设计更容易理解,所以 geeorm 接口设计上主要参考了 xorm,一些细节实现上参考了 gorm。

7天用Go从零实现RPC框架 GeeRPC

GeeRPC 是一个基于 net/rpc 开发的 RPC 框架 GeeRPC 是基于 Go 语言标准库 net/rpc 实现的,添加了协议交换、服务注册与发现、负载均衡等功能,代码约 1k。

WebAssembly 使用示例

具体的实践过程记录在 Go WebAssembly 简明教程。

  • 示例一:Hello World | Code
  • 示例二:注册函数 | Code
  • 示例三:操作 DOM | Code
  • 示例四:回调函数 | Code

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

Gee is a gin-like framework

  • 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

  • Demo 1 - Hello World Code
  • Demo 2 - Register Functions Code
  • Demo 3 - Manipulate DOM Code
  • Demo 4 - Callback Code