×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang bytes.Map() Function with Examples

Golang | bytes.Map() Function: Here, we are going to learn about the Map() function of the bytes package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 24, 2021

bytes.Map()

The Map() function is an inbuilt function of the bytes package which is used to get a copy of the byte slice (s) with all its characters modified according to the mapping function.

It accepts two parameters (mapping func(r rune) rune, s []byte) and returns a copy of the byte slice s with all its characters modified according to the mapping function.

Note:

  • If the mapping function returns a negative value, the character is dropped from the byte slice with no replacement.
  • The characters in byte slice (s) and the result are interpreted as UTF-8-encoded code points.

Syntax

Map(mapping func(r rune) rune, s []byte) []byte

Parameters

  • mapping : The map function to be used to modify the characters.
  • s : The byte slice in which we have to map the characters.

Return Value

The return type of the bytes.Map() function is a []byte, it returns a copy of the byte slice s with all its characters modified according to the mapping function.

Example 1

// Golang program to demonstrate the
// example of bytes.Map() function

package main

import (
	"bytes"
	"fmt"
	"unicode"
)

func main() {
	// Toggle characters
	f := func(c rune) rune {
		if unicode.IsUpper(c) == true {
			return unicode.ToLower(c)
		} else if unicode.IsLower(c) == true {
			return unicode.ToUpper(c)
		} else {
			return c
		}
	}

	fmt.Printf("%s\n", bytes.Map(f,
		[]byte("Hello, World!")))

	fmt.Printf("%s\n", bytes.Map(f,
		[]byte("Okay@123!Hello...")))
}

Output:

hELLO, wORLD!
oKAY@123!hELLO...

Example 2

// Golang program to demonstrate the
// example of bytes.Map() function

package main

import (
	"bytes"
	"fmt"
	"unicode"
)

func main() {
	// Converts to Uppercase
	f1 := func(c rune) rune {
		if unicode.IsLower(c) == true {
			return unicode.ToUpper(c)
		} else {
			return c
		}
	}

	// Converts to Lowercase
	f2 := func(c rune) rune {
		if unicode.IsUpper(c) == true {
			return unicode.ToLower(c)
		} else {
			return c
		}
	}

	// Increase character Unicode code point
	// value by 1
	f3 := func(c rune) rune {
		return c + 1
	}

	fmt.Printf("%s\n", bytes.Map(f1,
		[]byte("Hello, World!")))
	fmt.Printf("%s\n", bytes.Map(f2,
		[]byte("Hello, World!")))
	fmt.Printf("%s\n", bytes.Map(f3,
		[]byte("Hello, World!")))

	fmt.Printf("%s\n", bytes.Map(f1,
		[]byte("Okay@123!Hello...")))
	fmt.Printf("%s\n", bytes.Map(f2,
		[]byte("Okay@123!Hello...")))
	fmt.Printf("%s\n", bytes.Map(f3,
		[]byte("Okay@123!Hello...")))
}

Output:

HELLO, WORLD!
hello, world!
Ifmmp-!Xpsme"
OKAY@123!HELLO...
okay@123!hello...
PlbzA234"Ifmmp///

Golang bytes Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.