# 安星模块 (/zh/docs/go/star)

按出生数据取某一组星耀的落宫。



不排整盘、只想知道「禄存落在哪一宫」或「这张盘的杂耀怎么分布」时用这一层。

所有索引都是**宫位索引**：0 为寅宫，11 为丑宫。

## StarBirth [#starbirth]

按出生数据安星的入口共用这一个参数结构。

```go
type StarBirth struct {
    SolarDate  string
    TimeIndex  uint8
    Gender     string
    FixLeap    bool
    Language   string
    Config     *Config
    FromStem   string
    FromBranch string
}
```

| 字段                        | 类型        | 说明                    |
| ------------------------- | --------- | --------------------- |
| `SolarDate`               | `string`  | 公历日期，格式 `YYYY-M-D`    |
| `TimeIndex`               | `uint8`   | 时辰索引 0–12             |
| `Gender`                  | `string`  | 性别，决定长生与博士十二神的顺逆      |
| `FixLeap`                 | `bool`    | 是否修正闰月                |
| `Language`                | `string`  | 星耀名称的输出语言，留空取 `zh-CN` |
| `Config`                  | `*Config` | 排盘配置，`nil` 取默认        |
| `FromStem` / `FromBranch` | `string`  | 起五行局的干支；两者须同时给出       |

```go
birth := iztro.StarBirth{
    SolarDate: "2000-8-16",
    TimeIndex: 2,
    Gender:    iztro.GenderFemale,
    FixLeap:   true,
}
```

<Callout type="info">
  本页示例统一用默认的 `zh-CN`（`Language` 留空即取它），因此星名输出都是中文。
</Callout>

<Callout type="info" title="FromStem / FromBranch 只影响起五行局">
  两者同时给出后，五行局改由该干支推算，进而改变紫微天府落点与长生十二神。
  其余各组星的起法不受影响。用它可以取到中州派地盘、人盘的安星结果。
</Callout>

***

## GetStartIndex [#getstartindex]

**用途**　求紫微、天府的起始宫位。

**斗数含义**　紫微是全盘的锚点：由五行局与农历生日按「起紫微星诀」定位，
其余十三颗主星再依紫微与天府的位置铺开。天府与紫微的位置互为镜像。

**签名**

```go
func GetStartIndex(birth StarBirth) (StartIndex, error)
```

**返回值**　`StartIndex{ ZiweiIndex, TianfuIndex int }`。

**示例**

```go
s, _ := iztro.GetStartIndex(birth)
fmt.Printf("%+v\n", s)
```

**输出**

```text
{ZiweiIndex:4 TianfuIndex:8}
```

***

## 各组落宫索引 [#各组落宫索引]

以下六个入口形状一致：收 `StarBirth`，返回一个字段全是宫位索引的结构体。

| 函数                    | 返回类型               | 字段                                         | 起法依据              |
| --------------------- | ------------------ | ------------------------------------------ | ----------------- |
| `GetLuYangTuoMaIndex` | `LuYangTuoMaIndex` | `LuIndex` `YangIndex` `TuoIndex` `MaIndex` | 年干定禄存，禄前羊后陀；天马按年支 |
| `GetKuiYueIndex`      | `KuiYueIndex`      | `KuiIndex` `YueIndex`                      | 年干                |
| `GetChangQuIndex`     | `ChangQuIndex`     | `ChangIndex` `QuIndex`                     | 时支                |
| `GetKongJieIndex`     | `KongJieIndex`     | `KongIndex` `JieIndex`                     | 时支                |
| `GetTimelyStarIndex`  | `TimelyStarIndex`  | `TaifuIndex` `FenggaoIndex`                | 时支                |
| `GetLuanXiIndex`      | `LuanXiIndex`      | `HongluanIndex` `TianxiIndex`              | 年支                |

**示例**

```go
l, _ := iztro.GetLuYangTuoMaIndex(birth)
c, _ := iztro.GetChangQuIndex(birth)
lx, _ := iztro.GetLuanXiIndex(birth)

fmt.Printf("%+v\n%+v %+v\n", l, c, lx)
```

**输出**

```text
{LuIndex:6 YangIndex:7 TuoIndex:5 MaIndex:0}
{ChangIndex:6 QuIndex:4} {HongluanIndex:9 TianxiIndex:3}
```

擎羊在禄存前一格、陀罗在后一格，这是「禄前羊刃当，禄后陀罗府」的直接体现。

***

## GetDailyStarIndex / GetMonthlyStarIndex / GetYearlyStarIndex [#getdailystarindex--getmonthlystarindex--getyearlystarindex]

**用途**　取按日、按月、按年起的杂耀落宫。

**斗数含义**　杂耀按起法分组：日系星从辅星位置起初一顺数到生日；
月系星按农历月份定位；年系星最多，按年干或年支起。

**返回值**

| 函数                    | 返回类型               | 字段                                                                                                                                                                                                                                                                                                                                                                                                                  |
| --------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `GetDailyStarIndex`   | `DailyStarIndex`   | `SantaiIndex` `BazuoIndex` `EnguangIndex` `TianguiIndex`                                                                                                                                                                                                                                                                                                                                                            |
| `GetMonthlyStarIndex` | `MonthlyStarIndex` | `YuejieIndex` `TianyaoIndex` `TianxingIndex` `YinshaIndex` `TianyueIndex` `TianwuIndex`                                                                                                                                                                                                                                                                                                                             |
| `GetYearlyStarIndex`  | `YearlyStarIndex`  | 27 项：`XianchiIndex` `HuagaiIndex` `GuchenIndex` `GuasuIndex` `TiancaiIndex` `TianshouIndex` `TianchuIndex` `PosuiIndex` `FeilianIndex` `LongchiIndex` `FenggeIndex` `TiankuIndex` `TianxuIndex` `TianguanIndex` `TianfuIndex` `TiandeIndex` `YuedeIndex` `TiankongIndex` `JieluIndex` `KongwangIndex` `XunkongIndex` `TianshangIndex` `TianshiIndex` `JiekongIndex` `JieshaAdjIndex` `NianjieIndex` `DahaoAdjIndex` |

**示例**

```go
d, _ := iztro.GetDailyStarIndex(birth)
m, _ := iztro.GetMonthlyStarIndex(birth)

fmt.Printf("%+v\n%+v\n", d, m)
```

**输出**

```text
{SantaiIndex:0 BazuoIndex:10 EnguangIndex:9 TianguiIndex:7}
{YuejieIndex:0 TianyaoIndex:5 TianxingIndex:1 YinshaIndex:0 TianyueIndex:9 TianwuIndex:0}
```

**边界与陷阱**

<Accordions>
  <Accordion title="年系星的年支按 HoroscopeDivide 取">
    年系杂耀属流年神煞，取年支时用的是 `HoroscopeDivide` 而非 `YearDivide`。
    两个配置不同时，年系星与主星、辅星可能基于不同的年支——这是刻意的流派区分。
  </Accordion>

  <Accordion title="红鸾天喜不在 YearlyStarIndex 里">
    它们也属年系，但由 `GetLuanXiIndex` 单独给出。
  </Accordion>

  <Accordion title="JiekongIndex / JieshaAdjIndex / DahaoAdjIndex 是中州派专有">
    这三项只在 `Algorithm` 为中州派时进入盘面，替换掉截路、空亡与大耗的默认取法；
    默认派别下它们仍会被算出来，只是不安进宫位。
  </Accordion>
</Accordions>

***

## GetMajorStar / GetMinorStar / GetAdjectiveStar [#getmajorstar--getminorstar--getadjectivestar]

**用途**　取主星、辅星、杂耀在十二宫的完整分布。

**签名**

```go
func GetMajorStar(birth StarBirth) ([][]Star, error)
func GetMinorStar(birth StarBirth) ([][]Star, error)
func GetAdjectiveStar(birth StarBirth) ([][]Star, error)
```

**返回值**　十二项切片，按宫位索引排列。每项是该宫的 `Star` 切片（可能为空）。

**示例**

```go
major, _ := iztro.GetMajorStar(birth)

for i := 0; i < 5; i++ {
    names := []string{}
    for _, s := range major[i] {
        names = append(names, s.Name)
    }
    fmt.Println(i, names)
}
```

**输出**

```text
0 [武曲 天相]
1 [太阳 天梁]
2 [七杀]
3 [天机]
4 [紫微]
```

**边界与陷阱**

<Callout type="info">
  返回的 `Star` 带亮度与生年四化标记，与整盘排出的完全一致——
  它们走的是同一段代码。要取整盘的话直接用 `BySolar` 更省事。
</Callout>

***

## GetChangsheng12 / GetBoShi12 / GetYearly12 [#getchangsheng12--getboshi12--getyearly12]

**用途**　取四组十二神在十二宫的排列。

**斗数含义**　这四组各是十二个标记排满十二宫，每宫恰好一个：
长生十二神按五行局起、随性别与年支阴阳定顺逆；
博士十二神从禄存起、同样定顺逆；
岁前十二神从年支起顺行；将前十二神按年支三合组起。

**签名**

```go
func GetChangsheng12(birth StarBirth) ([]string, error)
func GetBoShi12(birth StarBirth) ([]string, error)
func GetYearly12(birth StarBirth) (Yearly12, error)
```

**返回值**　十二项标识切片，按宫位索引排列。
`GetYearly12` 返回 `Yearly12{ Suiqian12, Jiangqian12 []string }`。

**示例**

```go
cs, _ := iztro.GetChangsheng12(birth)
bs, _ := iztro.GetBoShi12(birth)
y, _ := iztro.GetYearly12(birth)

fmt.Println(cs[:4])
fmt.Println(bs[:4])
fmt.Println(y.Suiqian12[:4])
fmt.Println(y.Jiangqian12[:4])
```

**输出**

```text
[jue mu si bing]
[faylian zhoushu jiangjun xiaohao]
[diaoke bingfu suijian huiqi]
[suiyi xiishen huagai jiesha]
```

返回的是标识而非译名，要展示用 `Translate(key, language)`。

***

## GetChangsheng12StartIndex / GetJiangqian12StartIndex [#getchangsheng12startindex--getjiangqian12startindex]

**用途**　只取两组十二神的起始宫位，不排整组。

**斗数含义**　长生起点由五行局定：水二局长生在申、木三局在亥、金四局在巳、
土五局在申、火六局在寅。将星起点由年支三合组定：寅午戌年在午、申子辰年在子、
巳酉丑年在酉、亥卯未年在卯。

**签名**

```go
func GetChangsheng12StartIndex(fiveElementsClass string) (int, error)
func GetJiangqian12StartIndex(branchKey string) (int, error)
```

**返回值**　`int`，0–11。这两个函数不需要出生数据。

**示例**

```go
a, _ := iztro.GetChangsheng12StartIndex(iztro.ClassWater2nd)
b, _ := iztro.GetChangsheng12StartIndex(iztro.ClassFire6th)
c, _ := iztro.GetJiangqian12StartIndex(iztro.BranchZi)
d, _ := iztro.GetJiangqian12StartIndex(iztro.BranchWu)

fmt.Println(a, b, c, d)
```

**输出**

```text
6 0 10 4
```

水二局长生在申（索引 6），火六局在寅（索引 0）。

***

## GetHoroscopeStar [#gethoroscopestar]

**用途**　取某个运限层级的流耀分布。

**斗数含义**　流耀是随运限产生的十颗星：魁钺昌曲禄羊陀马鸾喜。
它们的落宫由该层级的干支决定，名字随层级变化。流年层级额外多一颗年解。

**签名**

```go
func GetHoroscopeStar(stemKey, branchKey, scope string, language Language) ([][]Star, error)
```

**参数**

| 参数          | 类型         | 必填 | 默认 | 说明        |
| ----------- | ---------- | -- | -- | --------- |
| `stemKey`   | `string`   | 是  | —  | 该层级的天干标识  |
| `branchKey` | `string`   | 是  | —  | 该层级的地支标识  |
| `scope`     | `string`   | 是  | —  | 运限层级，决定星名 |
| `language`  | `Language` | 是  | —  | 盘面语言      |

**返回值**　十二项切片，按宫位索引排列。

**各层级的星名对照**

| 本命 | 大限 | 流年 | 流月 | 流日 | 流时 |
| -- | -- | -- | -- | -- | -- |
| 天魁 | 运魁 | 流魁 | 月魁 | 日魁 | 时魁 |
| 天钺 | 运钺 | 流钺 | 月钺 | 日钺 | 时钺 |
| 文昌 | 运昌 | 流昌 | 月昌 | 日昌 | 时昌 |
| 文曲 | 运曲 | 流曲 | 月曲 | 日曲 | 时曲 |
| 禄存 | 运禄 | 流禄 | 月禄 | 日禄 | 时禄 |
| 擎羊 | 运羊 | 流羊 | 月羊 | 日羊 | 时羊 |
| 陀罗 | 运陀 | 流陀 | 月陀 | 日陀 | 时陀 |
| 天马 | 运马 | 流马 | 月马 | 日马 | 时马 |
| 红鸾 | 运鸾 | 流鸾 | 月鸾 | 日鸾 | 时鸾 |
| 天喜 | 运喜 | 流喜 | 月喜 | 日喜 | 时喜 |

标识形如 `yunlu`（运禄）、`liulu`（流禄）、`yuelu`（月禄）、`rilu`（日禄）、`shilu`（时禄）。

**示例**

```go
decadal, _ := iztro.GetHoroscopeStar(iztro.StemJia, iztro.BranchZi, iztro.ScopeDecadal, iztro.LanguageZhCN)

for i := 0; i < 4; i++ {
    names := []string{}
    for _, s := range decadal[i] {
        names = append(names, s.Name)
    }
    fmt.Println(i, names)
}
```

**输出**

```text
0 [运禄 运马]
1 [运羊 运鸾]
2 []
3 [运昌]
```

**边界与陷阱**

<Callout type="info" title="流年层级多一颗年解">
  `ScopeYearly` 的结果里额外含年解，按流年地支定位，安放在十颗流耀之前。
  其余层级没有这一颗。
</Callout>

***

## 低层落宫 [#低层落宫]

上面的函数都从出生数据起算，内部先推出年干支、命宫、修正后的农历月，再落宫。
这一组则直接收那些中间量，自建流程时可以复用。

| 函数                                                               | 收            | 出（字段全为宫位索引 `int`）                                     |
| ---------------------------------------------------------------- | ------------ | ----------------------------------------------------- |
| `GetZuoYouIndex(lunarMonth)`                                     | 修正后的农历月 1–12 | `ZuoYouIndex{ZuoIndex, YouIndex}`                     |
| `GetHuoLingIndex(branchKey, timeIndex)`                          | 年支、时辰        | `HuoLingIndex{HuoIndex, LingIndex}`                   |
| `GetHuagaiXianchiIndex(branchKey)`                               | 年支           | `HuagaiXianchiIndex{HuagaiIndex, XianchiIndex}`       |
| `GetGuGuaIndex(branchKey)`                                       | 年支           | `GuGuaIndex{GuchenIndex, GuasuIndex}`                 |
| `GetJieshaAdjIndex(branchKey)`                                   | 年支           | `int`，劫煞宫位索引                                          |
| `GetDahaoIndex(branchKey)`                                       | 年支           | `int`，大耗宫位索引                                          |
| `GetNianjieIndex(branchKey)`                                     | 年支           | `int`，年解宫位索引                                          |
| `GetTianshiTianshangIndex(gender, branchKey, soulIndex, config)` | 性别、年支、命宫索引   | `TianshiTianshangIndex{TianshangIndex, TianshiIndex}` |
| `GetChangQuIndexByHeavenlyStem(stemKey)`                         | 天干           | `ChangQuIndex{ChangIndex, QuIndex}`                   |

**示例**

```go
chart, _ := iztro.BySolar("2000-8-16", 2, iztro.GenderFemale, true, iztro.LanguageZhCN, nil)
yearBranch := chart.RawDates.ChineseDate.YearlyKeys[1]

hl, _ := iztro.GetHuoLingIndex(yearBranch, 2)
gg, _ := iztro.GetGuGuaIndex(yearBranch)
cq, _ := iztro.GetChangQuIndexByHeavenlyStem(iztro.StemJia)

fmt.Println(hl, gg, cq)
```

**输出**

```text
{2 10} {3 11} {3 7}
```

**边界与陷阱**

<Callout type="warn" title="农历月要先修正">
  `GetZuoYouIndex` 收的是修正闰月之后的月份，即 `FixLunarMonthIndex(...) + 1`，
  不是农历原始月份。闰月盘直接传原始月份会落错宫。
</Callout>

<Callout type="info" title="天伤天使分派别">
  `GetTianshiTianshangIndex` 的结果随 `config.Algorithm` 变：中州派在阴男阳女
  （生年地支阴阳与性别阴阳不同）时天伤天使对调，通行派不对调；`config` 传 `nil` 取默认。

  `GetChangQuIndexByHeavenlyStem` 按天干起昌曲，用于运限层级的流昌流曲；
  本命盘的文昌文曲按时支走 `GetChangQuIndex`。
</Callout>
