Go language implements machine size side judgment code sharing

  • 2020-05-05 11:22:37
  • OfStack

How does golang determine the size of the machine?

The following code is for reference:


package main  
 
import (  
    "fmt" 
    "unsafe" 
)  
 
const N int = int(unsafe.Sizeof(0))  
 
func main() {  
    x := 0x1234  
    p := unsafe.Pointer(&x)  
    p2 := (*[N]byte)(p)   
    if p2[0] == 0 {  
        fmt.Println(" Machine: big end ")  
    } else {  
        fmt.Println(" Machine: small end ")  
    }  


Related articles: