본문 바로가기

언어7

genesis Block 생성 package main import ( "crypto/sha256" "fmt" ) type block struct { data string hash string prevHash string } func main() { genesisBlock := block{"Genesis Block", "", ""} hash := sha256.Sum256([]byte(genesisBlock.data + genesisBlock.prevHash)) hexHash := fmt.Sprintf("%x", hash) genesisBlock.hash = hexHash fmt.Println(genesisBlock) } 블럭 구조체 설정하고 제네시스 블럭에는 데이터, 해쉬값, 직전 해쉬값이 들어가는데 제네시스이기에 직전 해쉬가 없다. .. 2022. 7. 21.
go test sample study package main import "fmt" func plus(a, b int, name string) (int, string) { return a + b, name } func main() { result, name := plus(2, 2, "gunwoo") fmt.Println(result, name) } package main import "fmt" func plus(a ... int) (int) { var total int // == total := 0 for _, item := range a { total += item } return total //_ => 무시 //index = int //item => main, 2, 3, 4, 5,6,7 //range는 반복가능한 것들을 반복하게 해준다 .. 2022. 7. 21.
If with a Twist variable expression : if-else 안에서 사용가능하고 안에서만 적용됌 package main import ( "fmt" ) func canIDrink(age int) bool { if koreanAge := age + 2; koreanAge 2022. 2. 13.
for, range, ...args loop range : array에 loop 를 적용 할 수 있게 해줌, for 안에서만 사용 가능하다. package main import ( "fmt" ) func supperAdd(numbers ...int) int { for number := range numbers { fmt.Println(number) } return 1 } func main() { supperAdd(1, 2, 3, 4, 5) } 0 1 2 3 4 range는 index를 주기 때문에 다음과 같이 작성하면 1 2 3 4 5 6 이 나온다 package main import ( "fmt" ) func supperAdd(numbers ...int) int { for index, number := range numbers { fmt.. 2022. 2. 13.
Functions part Two naked return package main import ( "fmt" "strings" ) func lenAndUpper(name string) (length int, upper string) { length = len(name) upper = strings.ToUpper(name) return } func main() { totalLength, upper := lenAndUpper("name") fmt.Println(totalLength, upper) } return할 variable을 명시하지 않아도 함수 부분에 직접 넣어줄 수 있다 defer function이 끝났을 때 추가적으로 무엇인가 동작 하게 할 수 있다. package main import ( "fmt" "strings" ) func .. 2022. 2. 13.
Functions part One package main import "fmt" func multiply(a, b int) int { return a * b } func main() { fmt.Println(multiply(3, 4)) } 함수 인자에 type을 설정해주고 return 값에도 type을 설정해주어야 한다. 여러 개의 return 값 반환 package main import ( "fmt" "strings" ) func lenAndupper(name string) (int, string) { return len(name), strings.ToUpper(name) } func main() { totalLength, Up := lenAndupper("gunwoo") fmt.Println(totalLength, Up) } 6 GU.. 2022. 2. 13.