should convert w (type want) to args instead of using struct literal
319
320 return test{
321 name: "output success",
322 args: args{ 323 vals: w.vals,
324 },
325 want: w,
should convert w (type want) to args instead of using struct literal
399
400 return test{
401 name: "output success",
402 args: args{ 403 format: w.format,
404 vals: w.vals,
405 },
should convert w (type want) to args instead of using struct literal
557
558 return test{
559 name: "output success",
560 args: args{ 561 format: w.format,
562 vals: w.vals,
563 },
should convert w (type want) to args instead of using struct literal
635
636 return test{
637 name: "output success",
638 args: args{ 639 vals: w.vals,
640 },
641 want: w,
should convert w (type want) to args instead of using struct literal
715
716 return test{
717 name: "output success",
718 args: args{ 719 format: w.format,
720 vals: w.vals,
721 },
should convert w (type want) to args instead of using struct literal
793
794 return test{
795 name: "output success",
796 args: args{ 797 vals: w.vals,
798 },
799 want: w,
should convert w (type want) to args instead of using struct literal
873
874 return test{
875 name: "output success",
876 args: args{ 877 format: w.format,
878 vals: w.vals,
879 },
should convert w (type want) to args instead of using struct literal
951
952 return test{
953 name: "output success",
954 args: args{ 955 vals: w.vals,
956 },
957 want: w,
should convert w (type want) to args instead of using struct literal
1031
1032 return test{
1033 name: "output success",
1034 args: args{1035 format: w.format,
1036 vals: w.vals,
1037 },
should convert w (type want) to args instead of using struct literal
477
478 return test{
479 name: "output success",
480 args: args{ 481 vals: w.vals,
482 },
483 want: w,
Description
Two struct types with identical fields can be converted, and hence a manual copy of struct fields could be avoided with the help of type conversion.
In older versions of Go, the fields had identical struct tags. Since Go 1.8; however, struct tags are ignored during conversions, and thus, it is not necessary to manually copy every field individually.
Bad practice
type T1 struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
}
// Tag names have an underscore (_) in middle
type T2 struct {
Field1 string `json:"field_1"`
Field2 string `json:"field_2"`
}
func foo() {
x := T1{
Field1: "field1",
Field2: "field2",
}
// Manual copying. Not required from Go 1.8 onwards
y := T2{
Field1: x.Field1,
Field2: x.Field2,
}
}
Recommended
type T1 struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
}
// Tag names have an underscore (_) in middle
type T2 struct {
Field1 string `json:"field_1"`
Field2 string `json:"field_2"`
}
func foo() {
x := T1{
Field1: "field1",
Field2: "field2",
}
// Removes the need to copy every field manually.
// Requires Go v1.8+
y := T2(x)
}