Problem overview
The function getFullNames receives two slices of strings, firstNames and lastNames. The name at position i in one slice belongs with the name at position i in the other. The function returns a new slice of full names, each one a first name, one space and a last name, in the original order.
For firstNames = ["John", "Andy", "Mary"] and lastNames = ["Doe", "Smith", "Johnson"], the result is ["John Doe", "Andy Smith", "Mary Johnson"].
Approach: one index, two slices
The two slices are linked only by position, so a loop that walks the first slice by value would lose track of where it is in the second. A loop over the indices solves that: for each i, read firstNames[i] and lastNames[i], join them, and add the result to an answer slice. The loop touches each position once, so the work grows in step with the number of names.
Step by step
- Start with an empty slice.
nameswill collect the results. Writing it as[]string{}gives a slice with length 0 that is ready to grow. - Loop over the indices.
for i := range firstNamesruns once per element and setsito 0, 1, 2 and so on. With a single variable,rangegives you the index, not the element. - Build the full name. The
+operator joins strings, and the" "in the middle supplies the single space the output requires. - Append it.
appendreturns the grown slice, so its result must be assigned back tonames. - Return the slice after the loop, when it holds every full name.
The solution in Go
func getFullNames(firstNames []string, lastNames []string) []string {
names := []string{}
for i := range firstNames {
name := firstNames[i] + " " + lastNames[i]
names = append(names, name)
}
return names
}
The Go details worth knowing:
rangewith one variable.for i := range firstNamesgives indices. To get both, writefor i, first := range firstNames. Go rejects unused variables, which is why you can't writefor i, first := rangeand then ignorefirst; use_for a value you don't need.:=declares and infers the type.names := []string{}andname := ...create new variables inside the function. Plain=only assigns to a variable that already exists.- Why
names = append(...). A slice is a small header pointing to an array. When the array is full,appendallocates a bigger one and returns a header that points to it. Callingappend(names, name)and discarding the result loses the new element, and Go reports that the result is unused. - Strings are immutable. The
+creates a new string each time. The two input slices are never changed.
Complexity
With n names, the loop runs n times. Each pass joins two short strings and appends, and appending is amortized constant time, because the underlying array grows in large steps instead of one element at a time. Time is O(n) if you count names as the unit of work. The result slice holds n strings, so extra space is O(n).
Edge cases and common mistakes
- Empty input. Two empty slices give an empty result, because the loop body never runs. A
nilslice works the same way:rangeover it does nothing. - Different lengths. The loop follows
firstNames. IflastNamesis shorter,lastNames[i]stops the program with an "index out of range" panic. For example,["a", "b"]with["x"]panics ati = 1. The lesson assumes both slices have the same length. - Forgetting the space.
firstNames[i] + lastNames[i]gives"JohnDoe". - Dropping the result of
append.append(names, name)on its own line does not compile. - Returning inside the loop. A
returnthere would end the function after the first name.
Testing
["John", "Andy", "Mary"]and["Doe", "Smith", "Johnson"]give["John Doe", "Andy Smith", "Mary Johnson"].["Ada"]and["Lovelace"]give["Ada Lovelace"].- Two empty slices give an empty slice.