1, math
Provides basic constants and mathematical functions
1. Absolute value of number
func Abs(x float64 ) float64 exceptional case Abs(±Inf) = +Inf Abs(NaN) = NaN
2.func Cbrt(x float64 ) float64
func Cbrt(x float64 ) float64
3.Floor returns the maximum integer value less than or equal to x.
func Floor(x float64 ) float64
4.Ceil returns the smallest integer value greater than or equal to x.
func Ceil(x float64 ) float64
5.Copysign returns a value with x size and y sign.
func Copysign(x, y float64 ) float64 package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Copysign(3.2, -1)) } output -3.20
6.Dim returns the maximum value of x-y or 0.
func Dim(x, y float64 ) float64 func main() { fmt.Printf("%.2f\n", math.Dim(4, -2)) fmt.Printf("%.2f\n", math.Dim(-4, 2)) } output 6.00 0.00
7.Max returns the greater of x or y.
func Max(x, y float64 ) float64
8. Function minimum ¶
func Min(x, y float64 ) float64
9.Mod returns the floating-point remainder of x/y. The size of the result is less than y, and its sign is consistent with that of x.
func Mod(x, y float64 ) float64
10.Modf returns integer and decimal floating-point numbers whose sum is f. Both values have the same sign as f.
func Modf(f float64 ) (int float64 , frac float64 ) int, frac := math.Modf(3.14) fmt.Printf("%.2f, %.2f\n", int, frac) 3.00, 0.14
11.Round returns the nearest integer, rounded from zero.
func Round(x float64 ) float64
12.Sqrt returns the square root of x.
func Sqrt(x float64) float64
12.Trunc returns the integer value of x.
func Trunc(x float64 ) float64
Returns the arccosine of x in radians
func Acos(x float64 ) float64 exceptional case Acos(x) = NaN if x < -1 or x > 1
Returns the inverse hyperbolic cosine of x.
func Acosh(x float64) float64
Asin returns the inverse sine of x in radians.
func Asin(x float64 ) float64
Asinh returns the inverse hyperbolic sine of x.
func Asinh(x float64 ) float64
2, Use of fmt package
type person struct { Name string `json:"name"` } var a = person{Name:"principal"} %v Default when printing structure fmt.Println(fmt.Sprintf("%#V ", a) recognize the value of the format, and add the field name with a plus sign (% + v) Input: fmt.Println(fmt.Sprintf("%v", a)) Output:{principal} %#Go syntax representation of v value Input: fmt.Println(fmt.Sprintf("%#v", a)) Output: main.person{Name:"principal"} %T Value type Go Grammatical representation Input: fmt.Println(fmt.Sprintf("%T", a)) Output: main.person %% Text percentage sign; No value consumed Input: fmt.Println(fmt.Sprintf("%%", a)) Output:%%!(EXTRA main.person={principal}) %t The word true or false Input: fmt.Println(fmt.Sprintf("%t", false)) Output: false %b Print binary %c Print int Numeric code corresponding to type %d Print hexadecimal data %0 Print octal data %O Print octal 0 o start %q A single quotation mark character text with Go Syntax safe escape. %x 16 Hexadecimal, in lowercase a-f %X 16 Hexadecimal, in uppercase A-F %U Unicode Format: U+1234;And“ U+%04X"identical %f Default width, default precision %9f Width 9, default precision %.2f Default width, precision 2 %9.2f Width 9, accuracy 2 %9.f Width 9, precision 0 fmt.Println(fmt.Sscanf(" 123456 7 ", "%5d%d%d", &s, &i, &c)) fmt.Println(fmt.Sscanf("12 34 567 ", "%d%d%d", &s, &i,&c)) If there is no space in the middle of the character, the sum is divided according to the number. If there is a space, it is divided according to the space
3, IO package
Provides basic operations of I/O primitives.
Copy data
func Copy(dst Writer, src Reader) (written int64, err error) Buffered replication func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)
CopyN copies n bytes (or until an error occurs) from src to dst. It returns the number of bytes copied and the oldest error encountered while copying. When returned, write = = n if and only if err == nil.
func CopyN(dst Writer, src Reader, n int64) (written int64, err error)
Pipe creates a synchronized memory pipe. It can be used to put the required io Reader code and need io Connect the writer's code
Reads and writes on the pipeline are matched one-to-one unless multiple reads are required to consume a single Write. That is, every Write to the PipeWriter will be blocked until it meets one or more reads from the PipeReader, which completely consume the written data. Data is directly copied from Write to the corresponding Read (or reads); No internal buffer.
func Pipe() (* PipeReader , * PipeWriter ) package main import ( "fmt" "io" "log" "os" ) func main() { r, w := io.Pipe() go func() { fmt.Fprint(w, "some io.Reader stream to be read\n") w.Close() }() if _, err := io.Copy(os.Stdout, r); err != nil { log.Fatal(err) } }
ReadAll reads from r until an error or EOF occurs and returns the data it reads.
func ReadAll(r Reader ) ([] byte , error )
ReadAtLeast reads from r to buf until at least min bytes are read. If the number of bytes read is small, the number of bytes copied and the error are returned. The error is EOF only if the byte is not read. If EOF occurs after reading less than min bytes, ReadAtLeast will return errunexpected EOF. If min is greater than the length of the buffer, ReadAtLeast returns ErrShortBuffer. When returning, n > = min if and only if err == nil. If r returns an error reading at least min bytes, the error is deleted.
func ReadAtLeast(r Reader , buf [] byte , min int ) (n int , err error ) ReadFull take len(buf) Byte from r Accurately read buf Yes. If the number of bytes read is small, the number of bytes copied and the error are returned. func ReadFull(r Reader , buf [] byte ) (n int , err error )
WriteString writes the contents of string s to W, which accepts a byte slice. If w implements a StringWriter, its WriteString method is called directly. Otherwise, w.Write will only be called once.
func WriteString(w Writer , s string ) (n int , err error )
read file
func (s * SectionReader ) Read(p [] byte ) (n int , err error ) func (s * SectionReader ) ReadAt(p [] byte , off int64 ) (n int , err error ) package main import ( "fmt" "io" "log" "strings" ) func main() { r := strings.NewReader("some io.Reader stream to be read\n") s := io.NewSectionReader(r, 5, 17) buf := make([]byte, 6) if _, err := s.ReadAt(buf, 10); err != nil { log.Fatal(err) } fmt.Printf("%s\n", buf) }
search
func (s * SectionReader ) Seek(offset int64 , whence int ) ( int64 , error ) package main import ( "io" "log" "os" "strings" ) func main() { r := strings.NewReader("some io.Reader stream to be read\n") s := io.NewSectionReader(r, 5, 17) if _, err := s.Seek(10, io.SeekStart); err != nil { log.Fatal(err) } if _, err := io.Copy(os.Stdout, s); err != nil { log.Fatal(err) } }
size
Size returns the size of the section in bytes.
func (s * SectionReader ) Size() int64