diff --git a/common/pool/alloc.go b/common/pool/alloc.go index bf177da..6ae53c1 100644 --- a/common/pool/alloc.go +++ b/common/pool/alloc.go @@ -8,11 +8,7 @@ import ( "sync" ) -var defaultAllocator *Allocator - -func init() { - defaultAllocator = NewAllocator() -} +var defaultAllocator = NewAllocator() // Allocator for incoming frames, optimized to prevent overwriting after zeroing type Allocator struct { diff --git a/component/process/process_linux.go b/component/process/process_linux.go index 1f651cd..fabe969 100644 --- a/component/process/process_linux.go +++ b/component/process/process_linux.go @@ -16,14 +16,14 @@ import ( ) // from https://github.com/vishvananda/netlink/blob/bca67dfc8220b44ef582c9da4e9172bf1c9ec973/nl/nl_linux.go#L52-L62 -func init() { +var nativeEndian = func() binary.ByteOrder { var x uint32 = 0x01020304 if *(*byte)(unsafe.Pointer(&x)) == 0x01 { - nativeEndian = binary.BigEndian - } else { - nativeEndian = binary.LittleEndian + return binary.BigEndian } -} + + return binary.LittleEndian +}() type SocketResolver func(network string, ip net.IP, srcPort int) (inode, uid int, err error) type ProcessNameResolver func(inode, uid int) (name string, err error) @@ -40,8 +40,6 @@ const ( pathProc = "/proc" ) -var nativeEndian binary.ByteOrder = binary.LittleEndian - func findProcessName(network string, ip net.IP, srcPort int) (string, error) { inode, uid, err := DefaultSocketResolver(network, ip, srcPort) if err != nil { diff --git a/constant/path.go b/constant/path.go index 021721e..ba0e8c2 100644 --- a/constant/path.go +++ b/constant/path.go @@ -9,21 +9,19 @@ import ( const Name = "clash" // Path is used to get the configuration path -var Path *path - -type path struct { - homeDir string - configFile string -} - -func init() { +var Path = func() *path { homeDir, err := os.UserHomeDir() if err != nil { homeDir, _ = os.Getwd() } homeDir = P.Join(homeDir, ".config", Name) - Path = &path{homeDir: homeDir, configFile: "config.yaml"} + return &path{homeDir: homeDir, configFile: "config.yaml"} +}() + +type path struct { + homeDir string + configFile string } // SetHomeDir is used to set the configuration path