Newline braces mean what in Go? [duplicate] - go

This question already has an answer here:
Statement blocks in go
(1 answer)
Closed 7 months ago.
when writing gin(a go web frame) code, I found a
piece of code like this:
r := gin.New()
apiv1 := r.Group("/api/v1")
{ // don't understand
apiv1.GET("/tags", v1.GetTags)
apiv1.POST("/tags", v1.AddTag)
}
It's ok and have no warn or error.
But I do don't know what's the newline braces mean, or it just has no effect?

From Go spec docs
blocks nest and influence scoping

Related

what's the purpose of loop at the end of go runtime main [duplicate]

This question already has an answer here:
Why is there a seemingly useless infinite for loop in the main function in src/runtime/proc.go?
(1 answer)
Closed 12 months ago.
when I read the runtime source, I find this code at the end of runtime/proc.go func main
exit(0)
for {
var x *int32
*x = 0
}
I check the history of the file.
I find that from the c-impl, it already is. runtime/proc.c
runtime·exit(0);
for(;;)
*(int32*)runtime·main = 0;
It confuses me.
Why there are some code after exit?
When the code will be executed?
It will panic directly, so Why it need a for-loop?
The comments on the commit give two reasons...
If exit is not implemented properly, dereferencing a null pointer will crash the program.
A for loop with no condition is a terminating statement, though I don't understand why this is necessary.

How to tell Go compiler to ignore unused variable? [duplicate]

This question already has answers here:
Go: Unused variable
(2 answers)
Closed 1 year ago.
I have code that loops through each rune of a string like so:
for i, character := range "abcdefghjklmnopqrstuv" {
fmt.Printf("character and i: ", character, i)
}
However, I have no need do anything with i. I only need that for the loop to work. If I leave i out of fmt.Printf, the compiler complains that I have defined something I did not use. If I leave i in, it clutters my console output.
How can I can tell the compiler to ignore the unused variable?
Use the blank identifier _:
for _, character := range "abcdefghjklmnopqrstuv" {
fmt.Printf("character: ", character)
}
This is covered in the Tour of Go.
I understand the confusion.
Unlike most program languages Go does not allow unused variables. This is because of a design decision to enforce that on this level unlike as a optional choice for each developer as other languages do.
So you can use the blank identifier as another answer mentioned but this is because of the way Go works.

What does this golang code do? [duplicate]

This question already has answers here:
What does an underscore and interface name after keyword var mean?
(2 answers)
Closed 5 years ago.
I was reading DigitalOcean's golang client. I noticed that they create an instance of their *Op struct in a _ variable. Example:
https://github.com/digitalocean/godo/blob/master/droplets.go#L32
var _ DropletsService = &DropletsServiceOp{}
Why is this line needed?
This line is a compile time check that *DropletsServiceOp satisfies the DropletsService interface.
The line has no effect on the execution of the program.
If you look at the blame on that file, at that line, you get a clue:
https://github.com/digitalocean/godo/blame/master/droplets.go#L32
It does a compile-time check that *DropletsServiceOp satisfies the DropletsService interface. Prior to the commit introducing that, they were doing this in their test suite.

What does _, mean? [duplicate]

This question already has answers here:
What is "_," (underscore comma) in a Go declaration?
(9 answers)
Closed 7 years ago.
I'm new to Go and came across this line of code while browsing through some other threads:
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err)
What does the _, after the if mean? Is it specifying that something will be assigned in the if condition (as it appears is happening with err)? I couldn't find an example of this syntax on the wiki and I'm very curious to see what it's used for.
Here's a link to the thread I was looking at if it helps:
How to check if a file exists in Go?
Because os.Stat returns two values, you have to have somewhere to receive those if you want any of them. The _ is a placeholder that essentially means "I don't care about this particular return value." Here, we only care to check the error, but don't need to do anything with the actual FileInfo Stat gives us.
The compiler will just throw that value away.

Assign multi-value to struct literal [duplicate]

This question already has answers here:
Multiple values in single-value context
(6 answers)
Closed 7 years ago.
Is there any way in Go to do this:
segment := Segment{
CumulativeDistanceMm: strconv.Atoi(record[9]),
Length: strconv.Atoi(record[1]),
LinkId: strconv.Atoi(record[8]),
SegmentId: strconv.Atoi(record[2]),
}
The error that I get is that strconv.Atoi returns multiple values so I can't assign it directly to the struct properties. If it was a variable I could use the underscore to ignore the second value. Can I do something similar for structs?
strconv.Atoi can fail and you have to deal with this failure. If such failures are absolutely impossible you would write a function func MustAtoi(s string) int which panics on failure and use that one in your struct initialization.
In Go doing some programming instead of using syntactical sugar or fancy syntax is common.
Most probably you should rethink your error handling.

Resources