# package unsafe
`import "unsafe"`
unsafe包提供了一些跳过go语言类型安全限制的操作。
## Index
* [type ArbitraryType](#ArbitraryType)
* [type Pointer](#Pointer)
* [func Sizeof(v ArbitraryType) uintptr](#Sizeof)
* [func Alignof(v ArbitraryType) uintptr](#Alignof)
* [func Offsetof(v ArbitraryType) uintptr](#Offsetof)
## type [ArbitraryType](https://github.com/golang/go/blob/master/src/unsafe/unsafe.go#L12 "View Source")
```
type ArbitraryType int
```
ArbitraryType在本文档里表示任意一种类型,但并非一个实际存在与unsafe包的类型。
## type [Pointer](https://github.com/golang/go/blob/master/src/unsafe/unsafe.go#L22 "View Source")
```
type Pointer *ArbitraryType
```
Pointer类型用于表示任意类型的指针。有4个特殊的只能用于Pointer类型的操作:
```
1) 任意类型的指针可以转换为一个Pointer类型值
2) 一个Pointer类型值可以转换为任意类型的指针
3) 一个uintptr类型值可以转换为一个Pointer类型值
4) 一个Pointer类型值可以转换为一个uintptr类型值
```
因此,Pointer类型允许程序绕过类型系统读写任意内存。使用它时必须谨慎。
## func [Sizeof](https://github.com/golang/go/blob/master/src/unsafe/unsafe.go#L27 "View Source")
```
func Sizeof(v ArbitraryType) uintptr
```
Sizeof返回类型v本身数据所占用的字节数。返回值是“顶层”的数据占有的字节数。例如,若v是一个切片,它会返回该切片描述符的大小,而非该切片底层引用的内存的大小。
## func [Alignof](https://github.com/golang/go/blob/master/src/unsafe/unsafe.go#L37 "View Source")
```
func Alignof(v ArbitraryType) uintptr
```
Alignof返回类型v的对齐方式(即类型v在内存中占用的字节数);若是结构体类型的字段的形式,它会返回字段f在该结构体中的对齐方式。
## func [Offsetof](https://github.com/golang/go/blob/master/src/unsafe/unsafe.go#L32 "View Source")
```
func Offsetof(v ArbitraryType) uintptr
```
Offsetof返回类型v所代表的结构体字段在结构体中的偏移量,它必须为结构体类型的字段的形式。换句话说,它返回该结构起始处与该字段起始处之间的字节数。
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe