@@ -35,7 +35,7 @@ func Sort(ports []Port, predicate func(i, j Port) bool) {
3535
3636type portMapEntry struct {
3737 port Port
38- binding PortBinding
38+ binding * PortBinding
3939 portInt int
4040 portProto string
4141}
@@ -51,7 +51,13 @@ func (s portMapSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
5151// 3. port with tcp protocol
5252func (s portMapSorter ) Less (i , j int ) bool {
5353 pi , pj := s [i ].portInt , s [j ].portInt
54- hpi , hpj := toInt (s [i ].binding .HostPort ), toInt (s [j ].binding .HostPort )
54+ var hpi , hpj int
55+ if s [i ].binding != nil {
56+ hpi = toInt (s [i ].binding .HostPort )
57+ }
58+ if s [j ].binding != nil {
59+ hpj = toInt (s [j ].binding .HostPort )
60+ }
5561 return hpi > hpj || pi > pj || (pi == pj && strings .EqualFold (s [i ].portProto , "tcp" ))
5662}
5763
@@ -63,8 +69,9 @@ func SortPortMap(ports []Port, bindings map[Port][]PortBinding) {
6369 portInt , portProto := p .Int (), p .Proto ()
6470 if binding , ok := bindings [p ]; ok && len (binding ) > 0 {
6571 for _ , b := range binding {
72+ b := b // capture loop variable for go < 1.22
6673 s = append (s , portMapEntry {
67- port : p , binding : b ,
74+ port : p , binding : & b ,
6875 portInt : portInt , portProto : portProto ,
6976 })
7077 }
@@ -90,8 +97,8 @@ func SortPortMap(ports []Port, bindings map[Port][]PortBinding) {
9097 i ++
9198 }
9299 // reorder bindings for this port
93- if _ , ok := bindings [ entry .port ]; ok {
94- bindings [entry .port ] = append (bindings [entry .port ], entry .binding )
100+ if entry .binding != nil {
101+ bindings [entry .port ] = append (bindings [entry .port ], * entry .binding )
95102 }
96103 }
97104}
0 commit comments