Open In App

reflect.Bytes() Function in Golang with Examples

Last Updated : 03 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.Bytes() Function in Golang is used to get Value underlying value. To access this function, one needs to imports the reflect package in the program.

Syntax:

func (v Value) Bytes() []byte

Parameters: This function does not accept any parameters.

Return Value: This function returns the v’s underlying value.

Below examples illustrate the use of the above method in Golang:

Example 1:




// Golang program to illustrate
// reflect.Bytes() Function
    
package main
    
import (
    "fmt"
    "reflect"
)
  
type vall[]uint8
  
// Main function 
func main() {
  
    // use of Bytes() method
    fmt.Println(reflect.ValueOf(vall{'A', 'B', 'C', 'D', 'E'}).Bytes())
}        


Output:

[65 66 67 68 69]

Example 2:




// Golang program to illustrate
// reflect.Bytes() Function
    
package main
    
import (
    "fmt"
    "reflect"
)
  
type val1 uint8
type slicee []val1 
  
// Main function 
func main() {
    var val = slicee{11, 12, 13}
      
    // use of Bytes() method
    va := reflect.ValueOf(val).Bytes()
    va[1] = 4
    fmt.Println(val)
}


Output:

[11 4 13]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads