Skip to content

Repository files navigation

yi - I Ching (Book of Changes) Library for Go

Go Reference

A Go library for I Ching (Book of Changes) hexagram calculation and divination, providing complete functionality for hexagram generation, transformation, Five Elements (WuXing), Sexagenary Cycle (JiaZi), Six Relations (LiuQin), Shi-Ying positions, 81 numerology, and hexagram interpretation.


Project Architecture

The project uses a clean layered architecture: public types in core/, all implementation hidden in internal/, root package provides the only public API surface. External code can only import yi and yi/core — implementation details are fully encapsulated.

yi/
├── yi.go # Public API - re-exports types, provides all entry points
├── yi_test.go # Integration tests
├── doc.go # Package declaration
├── core/ # Public types, constants, errors, data stores
│ ├── types.go # Type definitions (Gua, Yao, ZhouYi, Bagua, WuXing, Sex, etc.)
│ ├── constants.go # Constants (Bagua, YaoPosition, WuXing, LiuQin, etc.)
│ ├── errors.go # Error definitions
│ ├── store.go # Data store declarations
│ └── data_gen.go # Auto-generated data initialization
├── internal/ # Implementation details (not importable externally)
│ ├── gua/ # Hexagram operations & transformations
│ ├── qigua/ # Divination methods (coins, dayan, meihua, time, lunar)
│ ├── jiegua/ # Hexagram interpretation & formatting
│ ├── wuxing/ # Five Elements operations
│ ├── jiazi/ # Sexagenary Cycle
│ ├── liuqin/ # Six Relations
│ ├── shiying/ # Shi-Ying positions
│ ├── numerology/ # 81 Numerology
│ ├── wenyan/ # Wenyan commentary
│ └── i18n/ # Internationalization (Chinese/English)
├── data/ # JSON data sources
├── cmd/
│ ├── divine/ # CLI tool
│ └── generate/ # Code generator
├── docs/ # Documentation
└── skills/ # AI Agent skills

Note: Import github.com/godcong/yi for the public API. Types are also available via github.com/godcong/yi/core. Implementation packages under internal/ are not importable by external code.


Daily Hexagram Skill

Recommended usage — Integrate with your AI assistant for daily divination.

User says "tell my fortune" → AI collects info → Generates full fortune report (800+ words)

Installation

npx skills add https://github.com/godcong/yi --skill daily-hexagram

After installation, the AI will automatically detect and download the yi binary for your platform on first use (via built-in SKILL.md rules).

Manual binary installation (optional)

If automatic installation fails, run the install script manually:

# macOS / Linux
./scripts/install.sh
# Windows (PowerShell)
.\scripts\install.bat

Or compile from source:

go install github.com/godcong/yi/cmd/divine@latest
# Then copy the compiled binary to the skill's bin/ directory

Compatible AI Agents

This Skill follows the Agent Skills open standard and works with all supporting AI agents:

AgentInstall Command
Claude Codenpx skills add https://github.com/godcong/yi --skill daily-hexagram -a claude-code
Traenpx skills add https://github.com/godcong/yi --skill daily-hexagram -a trae
Cursornpx skills add https://github.com/godcong/yi --skill daily-hexagram -a cursor
Codexnpx skills add https://github.com/godcong/yi --skill daily-hexagram -a codex
Goosenpx skills add https://github.com/godcong/yi --skill daily-hexagram -a goose
Gemini CLInpx skills add https://github.com/godcong/yi --skill daily-hexagram -a gemini-cli
Roo Codenpx skills add https://github.com/godcong/yi --skill daily-hexagram -a roo
Windsurfnpx skills add https://github.com/godcong/yi --skill daily-hexagram -a windsurf
OthersOmit -a flag, interactively select target agent

See npx skills docs for the full list.

Usage

Simply talk to your AI assistant:

You sayWhat AI does
"tell my fortune", "daily horoscope"Collects name + birthday → Daily hexagram (same person, same day = same hexagram)
"try another one"Changes time and generates a new hexagram
"use coins"Switches to coin method
"use yarrow stalks"Switches to Dayan (yarrow) method
"plum blossom divination"Switches to Meihua method

First use: AI will ask your name and birthday. After that, it remembers. Same person on the same day always gets the same hexagram.

Report Contents

Overview → Hexagram name / symbol / auspiciousness + core imagery + fortune基调
Details → Primary / transformed / moving lines / nuclear / inverse / reverse hexagrams
8-Dim Fortune → Career / Love / Wealth / Exams / Health / Travel / Lawsuit / Home
Do's & Don'ts → Today's recommended actions / taboos
Guidance → Action guidance + lucky direction / number / color

Full report is at least 800 words.

Supported Methods

MethodCLIDescription
DailydailyDate-based only, same person same day fixed
TimetimeIncludes hour, different times yield different hexagrams
CoinscoinsSix coin tosses, most classic
DayandayanYarrow stalk method, most ancient
MeihuameihuaPlum blossom time-based
NumbernumberCustom upper/lower trigram numbers

Skill Directory Structure

daily-hexagram/
├── SKILL.md # AI instructions (data-driven + AI interpretation)
├── bin/yi # Divination program (compiled binary)
├── references/
│ └── data-format.md # JSON output structure reference
├── scripts/
│ ├── install.sh # Install script
│ └── install.bat
└── user_profile.json # User info (auto-created)

CLI Tool

Installation

go install github.com/godcong/yi/cmd/divine@latest

Usage

# Daily hexagram (requires -seed for personalization)
yi -method daily -seed "John" -format json
# Time-based hexagram
yi -method time -seed "John" -sex male -format json
# Coin method
yi -method coins -coins-seed 42
# Plum blossom method
yi -method meihua -seed "John" -year 2026 -month 5 -day 12
# Dayan (yarrow) method
yi -method dayan -dayan-seed 999
# Number-based hexagram
yi -method number -ben 3 -bian 5 -dong 2

All Flags

FlagDefaultDescription
-methodtimeMethod: time / daily / coins / meihua / dayan / number
-seed""User identifier (name/ID), required for daily and time
-formattextOutput format: text / json
-langzhOutput language: zh (Chinese) / en (English)
-sexmaleGender: male / female
-yearcurrent yearGregorian year
-monthcurrent monthGregorian month
-daycurrent dayGregorian day
-hourcurrent hourHour 0-23
-coins-seed0Coin method seed (0=random)
-dayan-seed0Dayan method seed
-ben-1Number method upper trigram (0-7)
-bian-1Number method lower trigram (0-7)
-dong0Number method moving line (0-5)
-versionPrint version

Go Library

Installation

go get github.com/godcong/yi

Quick Start

package main
import (
"fmt""github.com/godcong/yi"
)
funcmain() {
// Daily hexagram (same person, same day = same hexagram)zy:=yi.DivineByDailyHexagram(2026, 5, 13, "John")
result:=yi.JieGua(zy, yi.Male)
fmt.Println(yi.FormatJieGua(result))
// Coin methodzy2, coins:=yi.DivineByCoins(42)
fmt.Printf("Primary: %s, Transformed: %s\n", zy2.GetGua(yi.Ben).Ming, zy2.GetGua(yi.Bian).Ming)
// Dayan (yarrow) methodzy3, _:=yi.DivineByDayan(999)
// Plum blossom methodzy4:=yi.DivineByMeihua(3, 5, 2)
// Time-based hexagramzy5:=yi.DivineByTimeGua(yi.TimeGuaParams{
Year: 2024, Month: 6, Day: 15, Hour: 10,
})
// Quick hex from current timezy6:=yi.DivineByCurrentTime()
_=zy6
}

Daily Hexagram Function

// Same person, same day = same hexagramzy:=yi.DivineByDailyHexagram(2026, 5, 13, "John")
result:=yi.JieGua(zy, yi.Male)
// result.JieDu contains pre-built 8-dimension fortune + do's/don'tsfmt.Println(result.JieDu.ShiYe) // Career fortunefmt.Println(result.JieDu.CoreImage) // Core imageryfmt.Println(result.JieDu.Yi) // Do'sfmt.Println(result.JieDu.Ji) // Don'ts// result.WuXingInfo contains Five Elements lucky attributesfmt.Println(result.WuXingInfo.LuckyNumber) // Lucky numberfmt.Println(result.WuXingInfo.LuckyColor) // Lucky color

API Overview

Divination Functions

FunctionDescription
DivineByDailyHexagram(year, month, day int, personalSeed string) *ZhouYiDaily hexagram
DivineByCurrentTime(personalSeed ...string) *ZhouYiCurrent time hexagram
DivineByCoins(seed int64) (*ZhouYi, [6]CoinResult)Coin method
DivineByDayan(seed int64) (*ZhouYi, [6]DayanResult)Dayan (yarrow) method
DivineByMeihua(upperNum, lowerNum, dongYao int) *ZhouYiPlum blossom method
DivineByMeihuaTime(t time.Time, seeds ...string) (*ZhouYi, int, int, int, int)Plum blossom time
DivineByTimeGua(params TimeGuaParams, seed ...string) *ZhouYiGregorian time
DivineByTime(year, month, day, hour int, seed ...string) *ZhouYiTime-based divination
DivineByLunarTime(lunarYear, lunarMonth, lunarDay, shichenNum int) *ZhouYiLunar time
DivineByNumber(shang, xia int, bianYao ...int) *ZhouYiNumber-based
Divine(shang, xia Bagua, bianYao ...int) *ZhouYiTrigram-based

Interpretation

FunctionDescription
JieGua(zy *ZhouYi, sex Sex) *JieGuaResultMain interpretation function
JieGuaWithLang(zy *ZhouYi, sex Sex, lang Language) *JieGuaResultInterpretation with language
FormatJieGua(result *JieGuaResult) stringFormatted output (Chinese)
FormatJieGuaWithLang(result *JieGuaResult, lang Language) stringFormatted output with language

Interpretation Result Structure

JieGuaResult contains:

FieldDescription
BenGuaInfo / BianGuaInfoPrimary / Transformed hexagram {Ming, GuaName, Symbol, GuaYi, TuanText, XiangText, JiXiong, ...}
HuGuaInfo / CuoGuaInfo / ZongGuaInfoNuclear / Inverse / Reverse hexagrams
DongYaoPos / DongYaoText / DongYaoJiXiongMoving line position / text / auspiciousness
IsJi / JiXiongReasonOverall auspiciousness / reason
FenXi8-dimension interpretation [{Category, Content, JiXiong, Source}]
JieDuPre-built interpretation {CoreImage, ShiYe, AiQing, ..., Yi, Ji}
WuXingInfoFive Elements lucky info {WuXing, Direction, LuckyNumber, LuckyColor}

Hexagram Lookup & Judgment

FunctionDescription
GetGuaByIndex(index string) (*Gua, error)Lookup by index (e.g., "乾乾")
GetGuaByXu(xu int) (*Gua, error)Lookup by sequence number (1-64)
IsJi(zy *ZhouYi, sex Sex) boolCheck auspiciousness (standalone function)
FilterYao(zy *ZhouYi, sex Sex, filters ...string) boolFilter by auspiciousness
GetShiYing(g *Gua) *ShiYingInfoGet Shi-Ying info (standalone function)
GetGuaGong(g *Gua) BaguaGet palace attribution (standalone function)
GetGuaPosition(g *Gua) GuaPositionGet hexagram position

Five Elements & Six Relations

FunctionDescription
GetWuXingByBagua(bagua Bagua) WuXingTrigram → Five Elements
GetLiuQin(guaGongWX, yaoWX WuXing) LiuQinCalculate Six Relations
Sheng(wx WuXing) WuXingGenerating relation
Ke(wx WuXing) WuXingOvercoming relation
BeiSheng(wx WuXing) WuXingBeing generated
BeiKe(wx WuXing) WuXingBeing overcome

81 Numerology

FunctionDescription
GetDayan(n int) (*Dayan, error)Lookup numerology (1-81)
MustGetDayan(n int) DayanLookup numerology (panics on invalid)
Dayan.IsJi() / .IsXiong() / .IsBest()Auspiciousness check

Wenyan Commentary

FunctionDescription
GetWenYan(g *Gua) *WenYanDataGet Wenyan commentary
HasWenYan(g *Gua) boolCheck if Wenyan exists

Internationalization

FunctionDescription
JieGuaWithLang(zy, sex, lang)Interpretation with language
FormatJieGuaWithLang(result, lang)Formatted output with language
TranslateJiXiong(jx, lang)Translate auspiciousness
TranslateCategory(cat, lang)Translate category
TranslateYaoPos(pos, lang)Translate line position
TranslateBaguaName(name, lang)Translate trigram name
TranslateWuXing(name, lang)Translate Five Elements

Core Types

TypeDescription
Gua (alias Hexagram)Hexagram: name / meaning / Tuan / Xiang / six lines
Yao (alias Line)Line: line text / auspiciousness / female fate
ZhouYi (alias IChing)I Ching: five hexagram types + moving lines
Bagua (alias Trigram)Eight Trigrams: 0=Qian...7=Kun
WuXingFive Elements: Wood / Fire / Earth / Metal / Water
SexGender: Male / Female
LiuQinSix Relations: Parents / Siblings / Wealth / Offspring / Official
DayanDayan numerology: 1-81 auspiciousness details
JieGuaResultInterpretation result (includes JieDu + WuXingInfo)
GuaJieDuPre-built interpretation (8 dimensions + do's/don'ts)
WuXingInfoFive Elements lucky attributes
LanguageOutput language: LangZH / LangEN

Hexagram Type Constants

ConstantValueDescription
Ben0Primary hexagram
Bian1Transformed hexagram
Hu2Nuclear hexagram (lines 2-3-4 lower, 3-4-5 upper)
Cuo3Inverse hexagram (all yin/yang flipped)
Zong4Reverse hexagram (upside down)

Eight Trigrams Constants

ConstantValueWuXingName
Qian0MetalQian ☰ (Heaven)
Dui1MetalDui ☱ (Lake)
Li2FireLi ☲ (Fire)
Zhen3WoodZhen ☳ (Thunder)
Xun4WoodXun ☴ (Wind)
Kan5WaterKan ☵ (Water)
Gen6EarthGen ☶ (Mountain)
Kun7EarthKun ☷ (Earth)

Convention: This project marks Yang as 0, Yin as 1 (bit representation), which is opposite to intuition.


Data Generation

Data is stored in JSON files under data/:

  • data/gua.json — 64 hexagrams
  • data/tuan.json — Tuan (Judgment) texts
  • data/xiang.json — Xiang (Image) texts
  • data/wenyan.json — Wenyan (Commentary) texts
  • data/jiazi.json — Sexagenary Cycle (60 JiaZi)
  • data/jiegua.json — Hexagram interpretations (8-dimension fortune + do's/don'ts + core imagery)
  • data/guagong.json — Palace attribution

Regenerate core/data_gen.go:

go run ./cmd/generate/

The generator reads JSON files from data/ and outputs core/data_gen.go.


Documentation


License

MIT License

About

yi is a eight trigrams generate algorithm with zhouyi

Topics

Resources

Stars

34 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages