⛓️
Blockchain
  • Start!
  • Go_lang
    • Tutorial
    • 1. Banking
    • 2. Dictionary
    • 3. URL Checker
    • Useful Methods - Slice
    • Useful data structure
  • RUST
    • Start
    • Basic
    • Basic Programming Concepts
      • Variables and Mutability
  • Bitcoin
    • Start
    • Introduction
    • Transactions
  • GO-BITCOIN
    • Start
    • 1. Blocks & Blockchain
    • 2. Proof of work
    • 3. BadgerDB
    • 4. Transactions
    • 5. Wallet
    • 6. Adding Digital Signatures
  • COSMOS
    • 코스모스 SDK
    • 코스모스 SDK 실습 - nameservice
    • 코스모스 허브는 어떻게 사용하는가?
    • 코스모스 허브, 금융의 역사를 다시 쓰다
    • Tendermint
      • ABCI
      • Messages
  • Cosmos Tutorial
    • Tutorials
    • 1. Blog
    • Nameservice
    • [Starport] Escrow Account: Scavenge
    • [Starport] Inter-Blockchain Communication: Basics
    • Create an IBC Interchain Exchange module
      • Introduction
      • App Design
      • Initialize the Blockchain
      • Create the Order Book
  • Ethereum
    • Start
    • Gas
    • Oracle Problem
  • consensus
    • DPoS
    • PBFT
    • Network model
  • cryptosystem
    • 대칭키 암호
    • IPFS
  • Social token
    • Rally
    • DeSo
      • Bitclout
      • Deso: The Decentralized Social Network
      • Setting Up Your Dev Environment
      • Deso Code Walkthrough
      • Web3 Will Not Be Built on Smart Contracts
  • 재윤TV
    • Start
    • 유니스왑에 대해서 아라보자
      • Concept
      • V2 백서 분석
      • V2 코드 분석
Powered by GitBook
On this page
  • Slice 내부에 특정 값이 존재하는지 판단하는 메소드
  • Slice 내부에 특정 위치의 값을 제거하는 메소드
  • Slice 내부에 특정 값을 제거하는 메소드

Was this helpful?

  1. Go_lang

Useful Methods - Slice

Go lang에서 쓸만한 메소드 정리.

Slice 내부에 특정 값이 존재하는지 판단하는 메소드

func numberInSlice(a int, list []int) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

Slice 내부에 특정 위치의 값을 제거하는 메소드

순서가 상관없을 때

  • 제거할 값 자리에 slice의 마지막 값을 대입 후 마지막 값을 제거

// i index의 값을 제거한다. (순서 바)
func remove(s []int, i int) []int {
    s[i] = s[len(s)-1]
    return s[:len(s)-1]
}

순서가 유지되어야 할 때

  • 값을 제거하고 그 뒤의 모든 값을 shifting한다.

// i index의 값을 제거한다. (순서 유)
func remove(slice []int, s int) []int {
    return append(slice[:s], slice[s+1:]...)
}

Slice 내부에 특정 값을 제거하는 메소드

위의 특정 index의 값을 제거하는 메소드와 연계하여 사용

// s slice에서 가장 먼저 나온 v값을 제거한다. 
func removeVal(s []int, v int) []int {
    for i, val := range s{
        if val == v{
            s = remove(s, i)
            break
        }
    }
    return s
}

Previous3. URL CheckerNextUseful data structure

Last updated 3 years ago

Was this helpful?