Make slice last comma advantages

Hi, from below code why last comma advantages strings assigned, nor throwing error with it or without it; I don’t find any use. plz help me to understand.
usa:=make([]string,5,5)
usa=[]string{ Alabama, Alaska, Arizona, Arkansas, California,}
//usa=[]string{ Alabama, Alaska, Arizona, Arkansas, California}
fmt.Println(usa)
fmt.Println(len(usa))
fmt.Println(cap(usa))

Thanks,
Subbareddy Jangalapall

other below one is I just started my slice with 5 capacity, but it’s allowing more than 5 at initial assigned?
usa:=make([]string,5,5)
usa=[]string{ Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin, Wyoming}
fmt.Println(usa)
fmt.Println(len(usa))
fmt.Println(cap(usa))

The comma followed by an no-element permits you to spread this across multiple lines.
Therefore this does not compilre: Go Playground - The Go Programming Language

In your second example, you have replaced the value of variable “usa” with another value of same type. Both values are slices, so safe to do so. The string initialization used in the second case is a shortcut for several other lines of code.

not sure, I made my question clear; BTW - both examples are working for me no compiler issues
with first example: WITH trailing comma(,) and WITHOUT trailing comma(,) its working no issues; question is: why trailing comma(,) and its advantages??

with second example: not replacing & its initializing; I did short declaration “usa” slice with help of make of current capacity 5 and max capacity is also 5. that’s the make slice intention right! if yes why & how its allowing while initializing the 50 values; it basically more than max capacity.

This line both:

  1. declares a usa variable of type []string
  2. Allocates a []string slice of length 5 and capacity 5.

This line overwrites the []string slice in usa with length 5 from the previous line and writes a new slice with length 5 and the given state names into the usa var.

This line, if it were uncommented, does the same thing as the line above. The trailing comma doesn’t matter when your slice is defined on one line. The trailing comma is needed when the slice is spread across multiple lines.

In your second example:

This line both:

  1. declares a usa variable of type []string
  2. Allocates a []string slice of length 5 and capacity 5.

Just like your first example, this line overwrites the []string slice in usa with length 5 from the previous line and writes a new slice with length and capacity 50 and the given state names into the usa var.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.