'Struct Field Hash function with other Fields of the same Struct Set - GoLang
I'm new to GoLang and am starting with trying to build a simple blockchain. I am having trouble creating a hash of the blocks. Can anyone help me with how I could pass the other fields of the struct set into the Hash() function within the same struct, or if it needs to be outside of the stuct somehow, or if it's even possible...
Block Struct
type Block struct {
Index int
PrevHash string
Txs []Tx
Timestamp int64
Hash string
}
Set Struct Example
Block{
Index: 0,
PrevHash: "Genesis",
Txs: []Tx{},
Timestamp: time.Now().Unix(),
Hash: Hash(/* How do I pass the other fields data here... */),
}
My Hash Function
func Hash(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}
My Imports (if helpful)
import (
"crypto/md5"
"encoding/hex"
"fmt"
"time"
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
