전체 글
-
구글링으로 찾은 TypeScript 초보자를 위한 안전한 스토리지 래퍼 활용법Javascript 2024. 9. 20. 18:26
구글링으로 찾은 TypeScript 초보자를 위한 안전한 스토리지 래퍼 활용법 TypeScript는 JavaScript에 정적 타입을 더한 언어로, 코드의 안정성과 가독성을 높여주는 강력한 도구입니다. 최근 구글링을 통해 Type-Safe Storage Wrapper를 발견하게 되었습니다. 이 글에서는 제가 찾은 이 유용한 도구를 TypeScript 초보자 분들도 쉽게 이해하고 활용할 수 있도록 설명드리려고 합니다.예상 독자TypeScript를 막 배우기 시작한 분JavaScript의 웹 스토리지(localStorage, sessionStorage) 사용 시 타입 안전성을 높이고 싶은 분기본적인 TypeScript 문법과 제네릭에 대한 이해가 있는 분Web Storage란?Web Storage는 브라우저..
-
Go fmt.Scanner 완벽 가이드: 효율적인 데이터 파싱 기법Go 2024. 9. 20. 17:44
Go fmt.Scanner 완벽 가이드: 효율적인 데이터 파싱 기법Go 언어로 데이터를 파싱할 때, fmt.Scanner는 강력하고 유연한 도구입니다. 단순한 문자열 분리부터 복잡한 구조의 데이터 처리까지, fmt.Scanner를 활용하면 효율적이고 가독성 높은 코드를 작성할 수 있습니다. 이 글에서는 fmt.Scanner의 기본적인 사용법부터 HTTP 요청 파싱, 그리고 커스텀 스캐너 구현까지 다양한 예제와 함께 자세히 살펴보겠습니다. 기본적인 문자열 파싱Go에서 문자열을 파싱하는 방법은 여러 가지가 있습니다. 간단한 경우에는 strings.Split 함수로 충분하지만, 복잡한 형식이나 다양한 타입의 데이터를 처리해야 할 때는 fmt.Sscanf가 더 효율적일 수 있습니다. HTTP/1.0 요청 라인 ..
-
Go 이미지 생성: 완벽한 테스트 전략으로 버그 없는 코드 작성하기Go 2024. 9. 20. 17:32
Go 이미지 생성: 완벽한 테스트 전략으로 버그 없는 코드 작성하기Go 언어는 성능과 효율성으로 인정받는 언어지만, 이미지 생성과 같은 복잡한 작업에서는 예상치 못한 버그에 직면할 가능성이 높습니다. 라이브러리 업데이트나 작은 코드 변경에도 이미지 생성 결과가 달라질 수 있기 때문에, 철저한 테스트 전략은 필수적입니다. 이 글에서는 Go를 이용한 이미지 생성 과정에서 효과적인 테스트를 구현하는 방법을 다양한 예시와 함께 자세히 설명합니다. 단순히 테스트 코드 작성법을 넘어, 안정적이고 유지보수가 용이한 코드를 작성하는 데 필요한 전반적인 전략을 제시합니다.이미지 생성 함수 예시먼저, 테스트 대상이 될 간단한 이미지 생성 함수를 살펴보겠습니다. 이 함수는 입력 문자열을 이미지에 출력하고 JPEG 형식으로 ..
-
Go로 작성된 로컬 파일 처리 CLI 도구 테스트 방법 3가지Go 2024. 9. 20. 17:24
Go로 작성된 로컬 파일 처리 CLI 도구 테스트 방법 3가지Go 언어의 크로스 플랫폼 지원을 활용하여 로컬 파일을 처리하는 CLI 도구를 개발할 때, 효과적인 테스트 전략은 개발 생산성과 코드 품질에 직결됩니다. 파일 처리 로직이 복잡해질수록 테스트 코드 작성은 더욱 중요해지며, 적절한 테스트 패턴을 선택하는 것이 필수적입니다. 본 글에서는 Go로 작성된 CLI 도구의 파일 처리 로직을 테스트하기 위한 세 가지 패턴을 제시하고, 각 패턴의 장단점과 적용 시나리오를 자세히 설명합니다. 테스트 대상 코드 예시다음은 본 글에서 설명할 테스트 패턴을 적용할 예시 코드입니다. 이 코드는 입력 디렉토리(input)의 모든 텍스트 파일에 한 줄을 추가하고, 결과를 출력 디렉토리(output)에 별칭으로 저장하는 기..
-
Go 언어로 HTTP 서버 기본 구조 깔끔하게 잡기: `errgroup` 활용!Go 2024. 9. 20. 11:17
Go 언어로 HTTP 서버 기본 구조 깔끔하게 잡기: errgroup 활용!안녕하세요! 요즘 제가 자주 작성하는 Go 언어 HTTP 서버 기본 코드를 소개해드리려고 합니다. 코드를 보는 게 이해하기 더 빠를 테니 바로 전체 코드를 보여드릴게요!목표localhost:8888에서 HTTP 서버 실행SIGINT 신호를 받으면 HTTP 서버 Graceful Shutdown여러 곳에서 ctx.Done() 처리위 내용들을 혼동 없이 깔끔하게 작성package mainimport ( "context" "fmt" "net/http" "os" "os/signal" "syscall" "x/sync/errgroup")func main()..
-
Go 언어에서 nil 포인터로 인한 오류를 미리 잡는 방법: 정적 분석 도구 활용법Go 2024. 9. 20. 11:07
Go 언어에서 nil 포인터로 인한 오류를 미리 잡는 방법: 정적 분석 도구 활용법Go 언어에서 nil 포인터로 인한 오류를 정적 분석으로 잡아내는 방법Go 언어를 사용하다 보면 nil로 인한 오류는 자주 접하는 문제 중 하나인데요. 특히 nil 포인터와 nil 인터페이스의 차이로 인해 발생하는 문제는 디버깅하기 쉽지 않습니다. 이번 글에서는 정적 분석을 통해 이러한 오류를 미리 찾아내는 방법을 소개하려고 합니다. 이를 통해 코드에서 발생할 수 있는 잠재적인 문제를 미리 예방할 수 있겠죠.문제 상황: nil인데 nil이 아닌 이유먼저 아래 코드를 실행해보세요.package mainimport "fmt"type MyErr struct{}func (*MyErr) Error() string { retur..
-
Go 언어에서 Templ 또는 일반 템플릿: 무엇을 선택해야 할까요?Go 2024. 9. 19. 23:12
Go 언어에서 Templ 또는 일반 템플릿: 무엇을 선택해야 할까요?Go 언어로 웹 개발을 진행하다 보면 템플릿 엔진 선택에 대해 고민하게 되는데요. 오늘은 Templ과 기본 html/template 중 어떤 것을 사용하는 것이 좋을지 함께 알아볼까요?Templ이란 무엇인가요?Templ은 Go 언어를 위한 템플릿 엔진으로, 정적인 타입 검사와 컴파일 타임 오류 감지를 제공하여 안전하고 효율적인 템플릿 작성을 도와줍니다. 일반적인 템플릿 엔진보다 더욱 깔끔하고 유지보수가 쉬운 코드를 작성할 수 있다는 장점이 있는데요. 하지만 새로운 의존성을 추가하는 데에는 항상 고민이 따르는데요. 개발이 중단되거나 지원이 부족해질 수 있다는 우려 때문입니다.Templ의 장단점은 무엇일까요?장점컴파일 타임 검사: Templ..
-
Go 언어에서 구조체 필드를 반드시 지정하여 초기화하는 방법 알아볼까요?Go 2024. 9. 19. 22:49
Go 언어에서 구조체 필드를 반드시 지정하여 초기화하는 방법 알아볼까요?Go 언어를 사용하다 보면 구조체(struct)를 초기화할 때 모든 필드를 반드시 명시적으로 지정하도록 강제하고 싶은 경우가 있는데요. 기본적으로 Go에서는 구조체를 초기화할 때 필드를 생략하면 해당 필드들은 제로 값(zero value)으로 초기화됩니다. 즉, 포인터는 nil, 불리는 false, 문자열은 "" 등으로 자동 설정되는데요. 이는 때때로 개발자에게 의도치 않은 결과를 가져올 수 있습니다. 그렇다면 구조체를 인스턴스화할 때 모든 필드를 반드시 명시적으로 선언하도록 강제할 수 있는 방법이 있을까요? 오늘은 이 문제를 해결하기 위한 몇 가지 방법과, 왜 Go에서 이러한 동작을 기본으로 채택했는지에 대해 알아보겠습니다.필드를 ..