Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce anonymous structs #474

Merged
merged 1 commit into from
Jun 5, 2023
Merged

Introduce anonymous structs #474

merged 1 commit into from
Jun 5, 2023

Conversation

niyumard
Copy link
Contributor

@niyumard niyumard commented May 2, 2023

No description provided.

@@ -50,4 +50,15 @@ func main() {
// Structs are mutable.
sp.age = 51
fmt.Println(sp.age)

// Structs don't always have to be defined as instances of types,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, it's useful and I'm inclined to accept it.

Let's work out the details: I feel this comment is inaccurate because dog is in fact an instance of a type, just a type that isn't named. It should say something more to the tune of unnamed struct type (but also the word anonymous should be there.

Copy link
Contributor Author

@niyumard niyumard Jun 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eliben Sorry for the late response.
Which one do you think is better?

Structs don't always have to be defined as instances of named types, you can define a struct anonymously.

or

Structs can be instances of unnamed types, therefore you can define structs anonymously.

or we could simply go with "You can define structs anonymously".

Copy link
Collaborator

@eliben eliben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll approve this for now and will tweak the comment a bit

@eliben eliben merged commit 4b94115 into mmcgrana:master Jun 5, 2023
@niyumard
Copy link
Contributor Author

niyumard commented Jun 5, 2023

I'll approve this for now and will tweak the comment a bit

@eliben
Thanks! But I'd argue against mentioning "used for a single value" because it could be misleading. My main use of anonymous structs is for table driven tests where I can do something like this.
Maybe table driven tests should go hand in hand with #421 and have their own section in the site though.
Here's a code example that could be used in case you're interested in introducing slices of anonymous structs (which could be more useful):

dog := []struct {
	name   string
	isGood bool
}{
	{"Rex", true},
	{"Bruiser", false},
}
fmt.Println(dog)

Its result:
[{Rex true} {Teddy false}]

@eliben
Copy link
Collaborator

eliben commented Jun 5, 2023

We actually do have an example of table-driven tests already in https://gobyexample.com/testing-and-benchmarking -- I will add a link to it from this anonymous example.

"Value" is correct, even if it refers to a value of a slice type.

@niyumard
Copy link
Contributor Author

niyumard commented Jun 6, 2023

Looks great, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants