reflect.CanSet() Function in Golang with Examples

Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.CanSet() Function in Golang is used to check whether the value of v can be changed. To access this function, one needs to imports the reflect package in the program.

Syntax:

func (v Value) CanSet() bool

Parameters: This function does not accept any parameters.

Return Value: This function returns the boolean value.

Below examples illustrate the use of above method in Golang:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate
// reflect.CanSet() Function
  
package main
  
import (
    "reflect"
    "fmt"
  
)
  
type ProductionInfo struct {
    StructA []Entry
}
  
type Entry struct {
    Field1            string
    Field2            int
}
  
func SetField(source interface{}, fieldName string, fieldValue string){
    v := reflect.ValueOf(source)
    tt := reflect.TypeOf(source)
      
    for k := 0; k < tt.NumField(); k++ {
        fieldValue := reflect.ValueOf(v.Field(k))
          
        // use of CanSet() method
        fmt.Println(fieldValue.CanSet())
        if fieldValue.CanSet(){
            fieldValue.SetString(fieldValue.String())
        }
    }
}
  
// Main function
func main() {
    source := ProductionInfo{}
    source.StructA = append(source.StructA, Entry{Field1: "A", Field2: 2})
      
    SetField(source, "Field1", "NEW_VALUE")
}        

chevron_right


Output:

false

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate
// reflect.CanSet() Function
  
package main
  
import (
    "fmt"
    "reflect"
)
  
type ProductionInfo struct {
    StructA []Entry
}
  
type Entry struct {
    Field1 string
    Field2 int
}
  
func SetField(source interface{}, fieldName string, fieldValue string) {
    v := reflect.ValueOf(source).Elem()
      
    // use of CanSet() method
    fmt.Println(v.FieldByName(fieldName).CanSet())
  
    if v.FieldByName(fieldName).CanSet() {
        v.FieldByName(fieldName).SetString(fieldValue)
    }
}
  
// Main function
func main() {
    source := ProductionInfo{}
    source.StructA = append(source.StructA, Entry{Field1: "A", Field2: 2})
  
    fmt.Println("Before: ", source.StructA[0])
    SetField(&source.StructA[0], "Field1", "NEW_VALUE")
    fmt.Println("After: ", source.StructA[0])
}

chevron_right


Output:

Before:  {A 2}
true
After:  {NEW_VALUE 2}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.