💻
Solana Go
GithubMissing content?
  • 🏠Introduction
  • 🛠️Prepare
  • Tour
    • Intro
    • Create Account
    • Request Airdrop
    • Get Balance (SOL)
    • Transfer (SOL)
    • Create Mint (Token)
    • Create Account (Token)
      • Random Token Account
      • Associated Token Account
    • Mint To (Token)
    • Get Balance (Token)
    • Transfer (Token)
  • Advanced
    • Durable Nonce
      • Create Nonce Account
      • Get Nonce Account
      • Use Nonce
  • Metaplex (NFT)
    • Get Metadata
  • Misc
    • Need To Know
Powered by GitBook
On this page

Was this helpful?

  1. Tour

Create Account

You can use types.NewAccount to create a new account.

newAccount := types.NewAccount()
fmt.Println(newAccount.PublicKey.ToBase58())
fmt.Println(newAccount.PrivateKey)

If you already has private key, you can import it in different ways

types.AccountFromBase58("") // phantom exported private key 
	
types.AccountFromBytes([]bytes{}) // key.json file
	
types.AccountFromHex("")
package main

import (
	"fmt"
	"log"

	"github.com/portto/solana-go-sdk/types"
)

func main() {
	// create new account
	newAccount := types.NewAccount()
	fmt.Println(newAccount.PublicKey.ToBase58())
	fmt.Println(newAccount.PrivateKey)

	// recover account by its private key
	recoverAccount, err := types.AccountFromBytes(
		newAccount.PrivateKey,
	)
	if err != nil {
		log.Fatalf("failed to retrieve account from bytes, err: %v", err)
	}
	fmt.Println(recoverAccount.PublicKey.ToBase58())
}

PreviousIntroNextRequest Airdrop

Last updated 3 years ago

Was this helpful?