From 7246f25c07e99e316647e58f24f79824c9de0e27 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Sun, 11 Jul 2021 19:42:54 +0800 Subject: [PATCH] Chore: use iife replace init in some cases --- common/pool/alloc.go | 6 +----- component/process/process_linux.go | 12 +++++------- constant/path.go | 16 +++++++--------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/common/pool/alloc.go b/common/pool/alloc.go index bf177da8..6ae53c1d 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 1f651cd4..fabe969c 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 021721ec..ba0e8c23 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