Go Slice 테스트 코드

GoLang 2018. 9. 20. 14:24

package main


import (

"fmt"

)


func printSlice(a []int32)  {

fmt.Println(a)

fmt.Printf("%p, %d, %d\n", a, len(a), cap(a))

}


// slice의 원본과 사본에 동시 작업시에 문제를 확인하기 위한 코드

type ts struct {

s []int32

}


func testSliceInStruct()  {

t := &ts {

s: make([]int32, 0, 5),

}

t.s = append(t.s, 5)

a := t.s


printSlice(a)

printSlice(t.s)


a = append(a, 0, 1, 2)


printSlice(a)

printSlice(t.s)


b := &(t.s)

*b = append(*b, 2)


printSlice(a)

printSlice(t.s)

printSlice(*b)

}


// slice의 원본과 사본에 동시 작업시에 문제를 확인하기 위한 코드

var a = make([]int32, 0, 13)


func aaa(b int32) {

a = append(a, b)

}


func testSlice() {

a = append(a, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)


printSlice(a)


//*

b := a

for i := len(b) - 1; i >= 0; i-- {

// for i := 0; i < len(b); i++ {

if b[i] % 2 == 0 {

// 원본(a)에 작업

aaa(b[i])

// 사본(b)에 작업

b = append(b[:i], b[i+1:]...)

}

}

printSlice(b)

/*/

// 원본과 사본에 동시 작업 문제를 해결하기 위해

// 원본의 포인터를 가지고 사본 문제를 해결

b := &a

for i := len(*b) - 1; i >= 0; i-- {

// for i := 0; i < len(b); i++ {

if (*b)[i] % 2 == 0 {

aaa((*b)[i])

(*b) = append((*b)[:i], (*b)[i+1:]...)

}

}

printSlice(*b)

//*/

//a = b


printSlice(a)

}


// map의 value에 slice의 포인터를 넣어서 map 순행시에도 원본에 접근할 수 있게 함.

var m = make(map[int32]*[]int32)


func testMapNSlice() {

m[10] = &[]int32{}

*m[10] = make([]int32, 0, 10)

*m[10] = append(*m[10], 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

printSlice(*m[10])


for key, txs := range m {

for i := len(*txs) - 1; i >= 0; i-- {

if (*txs)[i] % 2 == 0 {

*m[key] = append(*m[key], (*txs)[i])

*txs = append((*txs)[:i], (*txs)[i+1:]...)

}

}


printSlice(*m[key])

printSlice(*txs)

}

}


func main()  {

//testSliceInStruct()

testSlice()

//testMapNSlice()

}




'GoLang' 카테고리의 다른 글

Go channel 테스트 코드  (0) 2018.09.20
블로그 이미지

영스파파

3D 세상을 만들기 위한 프로그래밍 정보들을 정리하는 공간

,

* 이더리움 리파지터리 설명

 

 -> 참조 : http://goodjoon.tistory.com/230?category=632200


* geth 빌드


 - git 설치


 - go lang 설치


 - mingw 설치


 - go lang의 src 폴더 밑에 geth 소스 받기

  = git clone https://github.com/ethereum/go-ethereum src\github.com\ethereum\go-ethereum

 

 - src\github.com\ethereum\go-ethereum 폴더로 이동해서 geth 빌드

  = go install -v ./cmd/...

 

 - go lang의 bin 폴더에 geth.exe 빌드되어 있음.


 -> 참조 : https://github.com/ethereum/go-ethereum/wiki/Installation-instructions-for-Windows

  http://softwaree.tistory.com/18


* geth 실행


 - init

  = .\geth.exe init ".\genesis.json" --datadir ".\node1"

  

 - 초기 계좌에 이더 지급

  = .\geth.exe --datadir ".\node1" account new

  = 참조 : https://github.com/ethereum/go-ethereum/issues/14831

  

 - geth 실행

  = .\geth.exe --datadir ".\node1" --networkid 25 console --nodiscover --rpc --rpcaddr "127.0.0.1" --rpcport "5000" --rpcapi "db,eth,net,personal,web3" --rpccorsdomain "http://172.20.50.149:5000" --port "30303"


 -> 참조 : https://steemit.com/kr/@sangphilkim/geth

  http://softwaree.tistory.com/18


* json rpc 인터페이스 테스트


 - postman 설치

 

 - 테스트

 

 -> 참조 : http://goodjoon.tistory.com/255

  https://github.com/ethereum/wiki/wiki/JSON-RPC


* 트렌젝션 리스트 얻기


 -> 참조 : https://ethereum.stackexchange.com/questions/2531/common-useful-javascript-snippets-for-geth/3478#3478


 

'블럭체인 개발' 카테고리의 다른 글

Nano 빌드  (0) 2018.09.20
블로그 이미지

영스파파

3D 세상을 만들기 위한 프로그래밍 정보들을 정리하는 공간

,

Unity Web Client

Unity/C# 2018. 1. 18. 16:37



using UnityEngine;

using UnityEngine.Networking;

using System.Collections;

using System.Text;

using BestHTTP;



public class Test : MonoBehaviour

{

bool _isSend = false;


// Use this for initialization

void Start()

{

StartCoroutine(CoExe());

}


IEnumerator CoExe()

{

//*

Send("http://127.0.0.1:3000/addUser/test1", "{\"password\":\"123456\",\"name\":\"789\"}", "POST");

yield return new WaitWhile(() => _isSend);

Send("http://127.0.0.1:3000/addUser/test2", "{\"password\":\"123456\",\"name\":\"789\"}", "POST");

yield return new WaitWhile(() => _isSend);

Send("http://127.0.0.1:3000/list", "", "GET");

yield return new WaitWhile(() => _isSend);


Send("http://127.0.0.1:3000/deleteUser/test1", "", "DELETE");

yield return new WaitWhile(() => _isSend);

Send("http://127.0.0.1:3000/deleteUser/test2", "", "DELETE");

yield return new WaitWhile(() => _isSend);

Send("http://127.0.0.1:3000/list", "", "GET");


yield break;

/*/

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/addUser/test1", "{\"password\":\"123456\",\"name\":\"789\"}", "POST"));

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/addUser/test2", "{\"password\":\"123456\",\"name\":\"789\"}", "POST"));

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/list", "", "GET"));


yield return StartCoroutine(CoSend("http://127.0.0.1:3000/deleteUser/test1", "", "DELETE"));

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/deleteUser/test2", "", "DELETE"));

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/list", "", "GET"));

//*/

}


void Send(string url, string body, string httpMethod)

{

_isSend = true;


HTTPMethods method = HTTPMethods.Get;

switch (httpMethod)

{

case "GET":

method = HTTPMethods.Get;

break;

case "POST":

method = HTTPMethods.Post;

break;

case "PUT":

method = HTTPMethods.Put;

break;

case "DELETE":

method = HTTPMethods.Delete;

break;

}


HTTPRequest request = new HTTPRequest(new System.Uri(url), method, Ack);

request.SetHeader("Content-Type", "application/json; charset=UTF-8");


request.RawData = Encoding.UTF8.GetBytes(body);

request.Send();

}


void Ack(HTTPRequest request, HTTPResponse response)

{

if (response != null)

{

Debug.LogError(request.Uri);

Debug.LogError(response.DataAsText);

}


_isSend = false;

}


IEnumerator CoSend(string url, string body, string httpMethod)

{

var request = new UnityWebRequest(url, httpMethod);

if (!string.IsNullOrEmpty(body))

{

byte[] bodyRaw = new UTF8Encoding().GetBytes(body);

request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);

}

request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");


yield return request.Send();


Debug.LogError(url);

Debug.LogError(request.downloadHandler.text);


request.Dispose();

}

}




'Unity > C#' 카테고리의 다른 글

Debug 교체  (0) 2016.03.17
Partial 클래스 정의(C# 프로그래밍 가이드)  (0) 2011.06.17
블로그 이미지

영스파파

3D 세상을 만들기 위한 프로그래밍 정보들을 정리하는 공간

,