Skip to main content
Fix the wrong type assignment
Source Link
sepehr
  • 5.8k
  • 3
  • 32
  • 34

This technique enforces keyed fields when declaring a struct.

For example, the struct:

type SomeType struct { Field1 string Field2 bool _ struct{} } 

can only be declared with keyed fields:

// ALLOWED: bar := SomeType{Field1: "hello", Field2: "true"true} // COMPILE ERROR: foo := SomeType{"hello", true} 

One reason for doing this is to allow additional fields to be added to the struct in the future without breaking existing code.

This technique enforces keyed fields when declaring a struct.

For example, the struct:

type SomeType struct { Field1 string Field2 bool _ struct{} } 

can only be declared with keyed fields:

// ALLOWED: bar := SomeType{Field1: "hello", Field2: "true"} // COMPILE ERROR: foo := SomeType{"hello", true} 

One reason for doing this is to allow additional fields to be added to the struct in the future without breaking existing code.

This technique enforces keyed fields when declaring a struct.

For example, the struct:

type SomeType struct { Field1 string Field2 bool _ struct{} } 

can only be declared with keyed fields:

// ALLOWED: bar := SomeType{Field1: "hello", Field2: true} // COMPILE ERROR: foo := SomeType{"hello", true} 

One reason for doing this is to allow additional fields to be added to the struct in the future without breaking existing code.

Source Link
Duncan Jones
  • 69.7k
  • 32
  • 204
  • 262

This technique enforces keyed fields when declaring a struct.

For example, the struct:

type SomeType struct { Field1 string Field2 bool _ struct{} } 

can only be declared with keyed fields:

// ALLOWED: bar := SomeType{Field1: "hello", Field2: "true"} // COMPILE ERROR: foo := SomeType{"hello", true} 

One reason for doing this is to allow additional fields to be added to the struct in the future without breaking existing code.