github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/create_file/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/hashgraph/hedera-sdk-go/v2"
     8  )
     9  
    10  func main() {
    11  	var client *hedera.Client
    12  	var err error
    13  
    14  	// Retrieving network type from environment variable HEDERA_NETWORK
    15  	client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    16  	if err != nil {
    17  		panic(fmt.Sprintf("%v : error creating client", err))
    18  	}
    19  
    20  	// Retrieving operator ID from environment variable OPERATOR_ID
    21  	operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    22  	if err != nil {
    23  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    24  	}
    25  
    26  	// Retrieving operator key from environment variable OPERATOR_KEY
    27  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    28  	if err != nil {
    29  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    30  	}
    31  
    32  	// Setting the client operator ID and key
    33  	client.SetOperator(operatorAccountID, operatorKey)
    34  
    35  	transactionResponse, err := hedera.NewFileCreateTransaction().
    36  		// A file is not implicitly owned by anyone, even the operator
    37  		// But we do use operator's key for this one
    38  		SetKeys(client.GetOperatorPublicKey()).
    39  		// Initial contents of the file
    40  		SetContents([]byte("Hello, World")).
    41  		// Optional memo
    42  		SetTransactionMemo("go sdk example create_file/main.go").
    43  		// Set max transaction fee just in case we are required to pay more
    44  		SetMaxTransactionFee(hedera.HbarFrom(8, hedera.HbarUnits.Hbar)).
    45  		Execute(client)
    46  
    47  	if err != nil {
    48  		panic(fmt.Sprintf("%v : error creating file", err))
    49  	}
    50  
    51  	// Make sure the transaction went through
    52  	transactionReceipt, err := transactionResponse.GetReceipt(client)
    53  
    54  	if err != nil {
    55  		panic(fmt.Sprintf("%v : error retrieving file create transaction receipt", err))
    56  	}
    57  
    58  	// Get and then display the file ID from the receipt
    59  	fmt.Printf("file = %v\n", *transactionReceipt.FileID)
    60  }