A Go module for parsing and working with chess games in PGN (Portable Game Notation) format.
go get github.com/Shobhit-Nagpal/pgn
package main
import (
"fmt"
"github.com/Shobhit-Nagpal/pgn"
)
func main() {
// Parse a PGN string
gameStr := `[Event "F/S Return Match"]
[Site "Belgrade, Serbia JUG"]
[Date "1992.11.04"]
[Round "29"]
[White "Fischer, Robert J."]
[Black "Spassky, Boris V."]
[Result "1/2-1/2"]
1. e4 e5 2. Nf3 Nc6 3. Bb5 1/2-1/2`
game, err := pgn.New(gameStr)
if err != nil {
panic(err)
}
// Access game information
fmt.Println("Event:", game.Event())
fmt.Println("White Player:", game.White())
fmt.Println("Black Player:", game.Black())
fmt.Println("Result:", game.Result())
}
- Parse PGN format chess games
- Access standard PGN tags (Event, Site, Date, Round, White, Black, Result)
- Custom tag support
- Move tracking
- Game result handling
New(pgn string) (*Game, error)
: Create a new game from PGN string
GetTag(name string) string
: Get the value of a specific tagSetTag(tag, value string)
: Set a tagTagPairs() map[string]string
: Get all tag pairs
Event() string
: Get the event nameSite() string
: Get the site/locationRound() string
: Get the round number/identifierDate() string
: Get the game dateWhite() string
: Get the white player's nameBlack() string
: Get the black player's name
Result() string
: Get the game resultSetResult(result string)
: Set the game resultIsDraw() bool
: Check if the game ended in a drawWinner() string
: Get the winner ("White", "Black", "Draw", or "Unknown")
GetMove(number int) *Move
: Get a specific move by numberSetMove(number int, move *Move)
: Set a move at a specific numberMoves() map[int]*Move
: Get all moves
Contributions are welcome! Please feel free to submit a Pull Request.
This module was made by reading the PGN specification provided by fsmosca.
This module is licensed under the MIT License - see the LICENSE file for details.