// 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 Ints.package bigimport ()// Gob codec version. Permits backward-compatible changes to the encoding.const intGobVersion byte = 1// GobEncode implements the [encoding/gob.GobEncoder] interface.func ( *Int) () ([]byte, error) {if == nil {returnnil, nil } := make([]byte, 1+len(.abs)*_S) // extra byte for version and sign bit := .abs.bytes() - 1// i >= 0 := intGobVersion << 1// make space for sign bitif .neg { |= 1 } [] = return [:], nil}// GobDecode implements the [encoding/gob.GobDecoder] interface.func ( *Int) ( []byte) error {iflen() == 0 {// Other side sent a nil or default value. * = Int{}returnnil } := [0]if >>1 != intGobVersion {returnfmt.Errorf("Int.GobDecode: encoding version %d not supported", >>1) } .neg = &1 != 0 .abs = .abs.setBytes([1:])returnnil}// AppendText implements the [encoding.TextAppender] interface.func ( *Int) ( []byte) ( []byte, error) {return .Append(, 10), nil}// MarshalText implements the [encoding.TextMarshaler] interface.func ( *Int) () ( []byte, error) {return .AppendText(nil)}// UnmarshalText implements the [encoding.TextUnmarshaler] interface.func ( *Int) ( []byte) error {if , := .setFromScanner(bytes.NewReader(), 0); ! {returnfmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", ) }returnnil}// The JSON marshalers are only here for API backward compatibility// (programs that explicitly look for these two methods). JSON works// fine with the TextMarshaler only.// MarshalJSON implements the [encoding/json.Marshaler] interface.func ( *Int) () ([]byte, error) {if == nil {return []byte("null"), nil }return .abs.itoa(.neg, 10), nil}// UnmarshalJSON implements the [encoding/json.Unmarshaler] interface.func ( *Int) ( []byte) error {// Ignore null, like in the main JSON package.ifstring() == "null" {returnnil }return .UnmarshalText()}
The pages are generated with Goldsv0.7.3-preview. (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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds.