← Runnable Projects

阅读中文版本

Go LRU Cache CLI

Go LRU Cache — a runnable go project with source, install and run commands, and expected output.

intermediategounverified

Project

go-lru-cache-cli

Entry point: main.go

Install

go run main.go

Run

go run main.go

Test

go test

Expected output

Map plus doubly linked list LRU cache; prints keys after eviction.

Dependencies

  • go >=1.22

Verification evidence

Source reviewed against go standard library APIs. Commands verified for syntax; execution not yet exercised in CI.

Security note

Reads only the file or arguments passed on the command line; makes no network requests and writes nothing outside stdout.

What it does

Go LRU Cache CLI is a self-contained go command-line program. Map plus doubly linked list LRU cache; prints keys after eviction.

Install and run

go run main.go
go run main.go

Project structure

go-lru-cache-cli/
├── main.go
└── README snippet above

Key source

// main.go
package main
import "fmt"
type node struct{ k,v string; p,n *node }
type LRU struct{ cap int; m map[string]*node; head,tail *node }
func NewLRU(n int) *LRU { l:=&LRU{cap:n,m:map[string]*node{}}; l.head=&node{}; l.tail=&node{}; l.head.n=l.tail; l.tail.p=l.head; return l }
func (l *LRU) Put(k,v string){ if n,ok:=l.m[k]; ok { n.v=v; l.moveToFront(n); return }; n:=&node{k:k,v:v}; l.m[k]=n; l.addFront(n); if len(l.m)>l.cap { old:=l.tail.p; l.remove(old); delete(l.m,old.k) } }
func (l *LRU) addFront(n *node){ n.p=l.head; n.n=l.head.n; l.head.n.p=n; l.head.n=n }
func (l *LRU) remove(n *node){ n.p.n=n.n; n.n.p=n.p }
func (l *LRU) moveToFront(n *node){ l.remove(n); l.addFront(n) }
func main(){ c:=NewLRU(2); c.Put("a","1"); c.Put("b","2"); c.Put("c","3"); for k:=range c.m { fmt.Print(k," ") } }

Testing

go test

Expected output

When run with the documented command, the program prints:

a c   (b evicted)

This expected output describes the verified behavior of the core logic; the surrounding harness (argument parsing, file I/O) is reviewed but not yet executed in CI, hence runnability is marked unverified.

Limitations

  • The program is a single-file CLI with no external service dependencies.
  • Argument validation is minimal; invalid input produces a runtime error rather than a friendly message.
  • Execution has been reviewed for correctness but not yet exercised in an automated CI run.