Skip to content

Commit 5c110c5

Browse files
committed
fix: 确保ListAll返回空切片而非nil
- 修复Linux/macOS/Windows平台的ListAll实现 - 使用make([]ProcessInfo, 0)初始化切片 - 避免在无进程时返回nil导致测试失败
1 parent c958b5c commit 5c110c5

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

pkg/port/darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (m *DarwinManager) ListAll() ([]ProcessInfo, error) {
129129
// parseLsofOutput 解析 lsof 输出
130130
func (m *DarwinManager) parseLsofOutput(output string) ([]ProcessInfo, error) {
131131
lines := strings.Split(output, "\n")
132-
var processes []ProcessInfo
132+
processes := make([]ProcessInfo, 0)
133133
seenPorts := make(map[string]bool) // 用于去重 (port+pid)
134134

135135
for _, line := range lines {

pkg/port/linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (m *LinuxManager) listAllWithNetstat() ([]ProcessInfo, error) {
175175
// parseSSOutput 解析 ss 命令输出
176176
func (m *LinuxManager) parseSSOutput(output string) ([]ProcessInfo, error) {
177177
lines := strings.Split(output, "\n")
178-
var processes []ProcessInfo
178+
processes := make([]ProcessInfo, 0)
179179
seenPorts := make(map[string]bool)
180180

181181
for _, line := range lines {
@@ -258,7 +258,7 @@ func (m *LinuxManager) parseSSOutput(output string) ([]ProcessInfo, error) {
258258
// parseNetstatOutput 解析 netstat 命令输出
259259
func (m *LinuxManager) parseNetstatOutput(output string) ([]ProcessInfo, error) {
260260
lines := strings.Split(output, "\n")
261-
var processes []ProcessInfo
261+
processes := make([]ProcessInfo, 0)
262262
seenPorts := make(map[string]bool)
263263

264264
for _, line := range lines {

pkg/port/windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (m *WindowsManager) ListAll() ([]ProcessInfo, error) {
137137
// parseNetstatOutput 解析 netstat 命令输出
138138
func (m *WindowsManager) parseNetstatOutput(output string) ([]ProcessInfo, error) {
139139
lines := strings.Split(output, "\n")
140-
var processes []ProcessInfo
140+
processes := make([]ProcessInfo, 0)
141141
seenPorts := make(map[string]bool) // 用于去重 (port+pid)
142142

143143
for _, line := range lines {

0 commit comments

Comments
 (0)