How to SHA256 a String in Golang

0 min read 61 words

If you need to SHA256 a String in Go, then you can use the crypto/sha256 package.

SHA256 a String in Go

package main

import (
	"crypto/sha256"
	"fmt"
)

func main() {
	s := "A sample string to SHA256!"

	h := sha256.New()

	h.Write([]byte(s))

	bs := h.Sum(nil)

	fmt.Println(s)
	fmt.Printf("%x\n", bs)
}

The output will look like this:

A sample string to SHA256!
9abf637c7e39cc4ef84d6d92cf7ffe168dc922b8ae666260d907e0353865ce89
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags