Merge Intervals(Golang)
Your code appears to fail on:

Example 1:

Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].


Output:
intervals : [[1 3] [2 6] [8 10] [15 18]]
comparing intervals  [1 3] and  [2 6]
comparing intervals  [2 6] and  [8 10]
comparing intervals  [8 10] and  [15 18]
merged intervals  [[1 6] [2 6] [8 10] [15 18]]


Try to simplify your code to make it more readable. For example,
package main

import (
    &quot;fmt&quot;
    &quot;sort&quot;
)

func merge(intervals [][]int) [][]int {
    const start, end = 0, 1

    var merged [][]int

    if len(intervals) &gt; 1 {
        sort.Slice(intervals, func(i, j int) bool {
            return intervals[i][start] &lt; intervals[j][start]
        })
    }

    for _, interval := range intervals {
        last := len(merged) - 1
        if last &lt; 0 || interval[start] &gt; merged[last][end] {
            merged = append(merged,
                []int{start: interval[start], end: interval[end]},
            )
        } else if interval[end] &gt; merged[last][end] {
            merged[last][end] = interval[end]
        }
    }

    return merged[:len(merged):len(merged)]
}

func main() {
    tests := []struct {
        intervals [][]int
    }{
        {[][]int{{1, 3}, {2, 6}, {8, 10}, {15, 18}}},
        {[][]int{{1, 4}, {4, 5}}},
        {[][]int{{1, 2}}},
        {[][]int{}},
        {[][]int{{7, 10}, {3, 4}, {2, 5}}},
    }

    for _, tt := range tests {
        fmt.Print(tt.intervals)
        fmt.Println(&quot; -&gt;&quot;, merge(tt.intervals))
    }
}

Playground: https://play.golang.org/p/Akgsws42JfX
Output:
[[1 3] [2 6] [8 10] [15 18]] -&gt; [[1 6] [8 10] [15 18]]
[[1 4] [4 5]] -&gt; [[1 5]]
[[1 2]] -&gt; [[1 2]]
[] -&gt; []
[[7 10] [3 4] [2 5]] -&gt; [[2 5] [7 10]]