// Copyright 2015 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// This file implements encoding/decoding of Rats.package bigimport ()// Gob codec version. Permits backward-compatible changes to the encoding.constratGobVersionbyte = 1// GobEncode implements the gob.GobEncoder interface.func ( *Rat) () ([]byte, error) {if == nil {returnnil, nil } := make([]byte, 1+4+(len(.a.abs)+len(.b.abs))*_S) // extra bytes for version and sign bit (1), and numerator length (4) := .b.abs.bytes() := .a.abs.bytes([:]) := - ifint(uint32()) != {// this should never happenreturnnil, errors.New("Rat.GobEncode: numerator too large") }binary.BigEndian.PutUint32([-4:], uint32()) -= 1 + 4 := ratGobVersion << 1// make space for sign bitif .a.neg { |= 1 } [] = return [:], nil}// GobDecode implements the gob.GobDecoder interface.func ( *Rat) ( []byte) error {iflen() == 0 {// Other side sent a nil or default value. * = Rat{}returnnil }iflen() < 5 {returnerrors.New("Rat.GobDecode: buffer too small") } := [0]if >>1 != ratGobVersion {returnfmt.Errorf("Rat.GobDecode: encoding version %d not supported", >>1) }const = 1 + 4 := binary.BigEndian.Uint32([-4 : ])ifuint64() > math.MaxInt- {returnerrors.New("Rat.GobDecode: invalid length") } := + int()iflen() < {returnerrors.New("Rat.GobDecode: buffer too small") } .a.neg = &1 != 0 .a.abs = .a.abs.setBytes([:]) .b.abs = .b.abs.setBytes([:])returnnil}// MarshalText implements the encoding.TextMarshaler interface.func ( *Rat) () ( []byte, error) {if .IsInt() {return .a.MarshalText() }return .marshal(), nil}// UnmarshalText implements the encoding.TextUnmarshaler interface.func ( *Rat) ( []byte) error {// TODO(gri): get rid of the []byte/string conversionif , := .SetString(string()); ! {returnfmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", ) }returnnil}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.