github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/construct_client/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 // Create client for previewnet 12 previewnetClient := hedera.ClientForPreviewnet() 13 // Create client for testnet 14 testnetClient := hedera.ClientForTestnet() 15 // Create client for mainnet 16 mainnetClient := hedera.ClientForMainnet() 17 18 println("Client Construction Example.") 19 20 // Creating client from the set HEDERA_NETWORK environment variable 21 namedNetworkClient, err := hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 22 if err != nil { 23 panic(fmt.Sprintf("%v : error creating client for name", err)) 24 } 25 26 // Creating account ID of 0.0.3 27 id, err := hedera.AccountIDFromString("0.0.3") 28 if err != nil { 29 panic(fmt.Sprintf("%v : error creating AccountID from string", err)) 30 } 31 32 // Creating a PrivateKey from a random key string we have 33 key, err := hedera.PrivateKeyFromString("302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10") 34 if err != nil { 35 panic(fmt.Sprintf("%v : error creating PrivateKey from string", err)) 36 } 37 38 // Set the operators for each client 39 testnetClient.SetOperator(id, key) 40 mainnetClient.SetOperator(id, key) 41 previewnetClient.SetOperator(id, key) 42 namedNetworkClient.SetOperator(id, key) 43 44 // Create the network map to use 45 customNetwork := map[string]hedera.AccountID{ 46 "2.testnet.hedera.com:50211": {Account: 5}, 47 "3.testnet.hedera.com:50211": {Account: 6}, 48 } 49 50 // Set network for customClient which uses the above custom network 51 customClient := hedera.ClientForNetwork(customNetwork) 52 // Setting NetworkName for the CustomClient, is only needed if you need to validate ID checksums 53 customClient.SetNetworkName(hedera.NetworkNameTestnet) 54 55 if os.Getenv("CONFIG_FILE") != "" { 56 // Creating client from a file specified in environment variable CONFIG_FILE 57 configClient, err := hedera.ClientFromConfigFile(os.Getenv("CONFIG_FILE")) 58 if err != nil { 59 panic(fmt.Sprintf("%v : error creating Client from config file", err)) 60 } 61 62 // Closing the client from file 63 err = configClient.Close() 64 if err != nil { 65 panic(fmt.Sprintf("%v : error closing configClient", err)) 66 } 67 } 68 69 // Clean up, closing each client 70 // Can also do this by using defer in after setting up the client 71 err = previewnetClient.Close() 72 if err != nil { 73 panic(fmt.Sprintf("%v : error closing previewnetClient", err)) 74 } 75 err = testnetClient.Close() 76 if err != nil { 77 panic(fmt.Sprintf("%v : error closing testnetClient", err)) 78 } 79 err = mainnetClient.Close() 80 if err != nil { 81 panic(fmt.Sprintf("%v : error closing mainnetClient", err)) 82 } 83 err = namedNetworkClient.Close() 84 if err != nil { 85 panic(fmt.Sprintf("%v : error closing namedNetworkClient", err)) 86 } 87 err = customClient.Close() 88 if err != nil { 89 panic(fmt.Sprintf("%v : error closing customClient", err)) 90 } 91 92 println("Success!") 93 }