range Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/range/ Fri, 26 Feb 2021 18:50:25 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 range Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/range/ 32 32 159787465 Validate the range of the integer in a struct in Go (Golang) https://vikasboss.github.io/range-int-struct-validate-golang/ https://vikasboss.github.io/range-int-struct-validate-golang/#respond Fri, 26 Feb 2021 18:50:16 +0000 https://vikasboss.github.io/?p=5162 Overview The below library can be used to validate the range of an integer in a struct in Golang gopkg.in/go-playground/validator.v9 – https://pkg.go.dev/github.com/go-playground/validator For this tutorial, we will use the below employee struct...

The post Validate the range of the integer in a struct in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

The below library can be used to validate the range of an integer in a struct in Golang

For this tutorial, we will use the below employee struct

type employee struct {
    Age int
}

Example

Let’s see an example for the same. Below is the code

go.mod

module sample.com/validate
go 1.14
require (
    github.com/go-playground/universal-translator v0.17.0 // indirect
    github.com/leodido/go-urn v1.2.1 // indirect
    gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
    gopkg.in/go-playground/validator.v9 v9.31.0
)

main.go

package main
import (
    "fmt"
    "gopkg.in/go-playground/validator.v9"
)
var validate *validator.Validate
type employee struct {
    Age int `validate:"required,gte=10,lte=20"`
}
func main() {
    e := employee{}
    err := validateStruct(e)
    if err != nil {
        fmt.Printf("Error: %s\n", err)
    }
    e = employee{Age: 5}
    err = validateStruct(e)
    if err != nil {
        fmt.Printf("Error: %s\n", err)
    }
    e = employee{Age: 25}
    err = validateStruct(e)
    if err != nil {
        fmt.Printf("Error: %s\n", err)
    }
}
func validateStruct(e employee) error {
    validate = validator.New()
    err := validate.Struct(e)
    if err != nil {
        return err
    }
    return nil
}

Output

Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'required' tag
Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'gte' tag
Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'lte' tag

First, we need to declare the instance of Validate

var validate *validator.Validate

Notice here that we need to associate meta tags with fields of the struct to let the validator know that you want to validate this field. In the above example, we added the tag with the Age field. This tag is interpreted by the playground validate library. Notice we added three validations for the Age field

  • required – validates that the field is present
  • gte – validate that the field value is greater than equal to a particular value
  • lte – validate that the field value is less than equal to a particular value
type employee struct {
	Age int `validate:"required,gte=10,lte=20"`
}

Then call the Struct method to validate the struct

validate.Struct(e)

For

e := employee{}

it gives the output as below is Age field is empty

Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'required' tag

For

e := employee{Age: 5}

it gives the output as below as Age field value is 5 which is less than 10

Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'gte' tag

For

e := employee{Age: 25}

it gives the output as below as Age field value is 25 which is greater than 20

Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'lte' tag

The post Validate the range of the integer in a struct in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/range-int-struct-validate-golang/feed/ 0 5162
Know Size and Range of int or uint in Go (Golang) https://vikasboss.github.io/go-size-range-int-uint/ https://vikasboss.github.io/go-size-range-int-uint/#respond Wed, 01 Jan 2020 17:53:13 +0000 https://vikasboss.github.io/?p=1062 Overview int is a signed Integer data type uint is an unsigned Integer data type Size and range of int and uint in go is platform-dependent meaning that the size and range...

The post Know Size and Range of int or uint in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview
  • int is a signed Integer data type
  • uint is an unsigned Integer data type

Size and range of int and uint in go is platform-dependent meaning that the size and range depend whether the underlying platform is 32-bit or a 64-bit machine.

Size Table

TypeSize (32 bit machine)Size (64 bit machine)
int32 bits or 4 byte64 bits or 8 byte
uint32 bits or 4 byte64 bits or 8 byte

Range Table

TypeSize (32 bit machine)Size (64 bit machine)
int-231 to 231 -1-263 to 263 -1
uint0 to 232 -10 to 264 -1

Know Size and Range

  • bits package of golang can help know the size of an int or uint on your system. bits.UintSize is the const that stores this value. It is calculated as below
const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
  • unsafe.Sizeof() function can also be used to see the size of int or unit in bytes

Once the size is known, the range can be deduced based upon size. See the below code for printing size.

package main

import (
    "fmt"
    "math/bits"
    "unsafe"
)

func main() {
    //This is computed as 
    //const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
    sizeInBits := bits.UintSize
    fmt.Printf("%d bits\n", sizeInBits)

    //Using unsafe.Sizeof() function. It will print size in bytes
    var a int
    fmt.Printf("%d bytes\n", unsafe.Sizeof(a))

    //Using unsafe.Sizeof() function. It will print size in bytes
    var b uint
    fmt.Printf("%d bytes\n", unsafe.Sizeof(b))
}

Output:

64 bits
8 bytes
8 bytes

The post Know Size and Range of int or uint in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/go-size-range-int-uint/feed/ 0 1062