기본 콘텐츠로 건너뛰기

golang에서 constance 정의하는 법- iota 사용하기







 상수정의시  첫번째 상수변수에 지정하면 자동으로 숫자를 배정한다.
const (
    CategoryBooks = iota // 0
    CategoryHealth       // 1
    CategoryClothing     // 2
)

package main 
import (
    "fmt"
)
type AudioOutput int
const (
    OutMute AudioOutput = iota // 0
    OutMono                    // 1
    OutStereo                  // 2
    _                             // 3과 4는 무시한다.
    _
    OutSurround                // 5
)

// 비트 연산자를 이용한 할당.

//<<는 숫자를 하나씩 좌측으로 이동한다.​
type Allergen int
const (
    IgEggs Allergen = 1 << iota          // 1 << 0 which is 00000001
    IgChocolate                         // 1 << 1 which is 00000010
    IgNuts                              // 1 << 2 which is 00000100
    IgStrawberries                      // 1 << 3 which is 00001000
    IgShellfish                         // 1 << 4 which is 00010000
)
func main() {
    fmt.Println(" OutMute  ", OutMute)
    fmt.Println(" OutSurround ", OutSurround)
    
    fmt.Println(" IgNuts       ", IgNuts  )
}
​// 결과값
 OutMute   0
 OutSurround  5
 IgNuts        4
 

댓글

이 블로그의 인기 게시물

golang overload 처럼 처리하기

go 언어에서는 함수명이 같을 경우 재선언 오류를 발생시킨다. 다른 메소드를 만들고 오버로드 처리하도록 설계해서 구현해야 한다. 오버로드 처리를 위해 type switch를 사용해서 들어오는 타입에 따라 메소드 호출하는 처리를 사용한다. 함수나 메소드 인자를 다양하게 처리하려면 []interface{} 로 정의해서 다양한 인자를 받아서 오버로딩 처리하면된다. package main import (  "fmt" ) func main() {  var p Person  p = Person{"dahl"}  var s string = "moon"  fmt.Println(" overloading  ", p.Overload(s))  fmt.Println(" overloading", p.Overload(nil)) } type Person struct {  name string } func (this *Person) Overload(args interface{}) string {  switch args.(type) {  case string:   return this.GetName2("moon")  case nil:   return this.GetName1()  default:   return " no match"  } } func (this *Person) GetName1() string {  return this.name } func (this *Person) GetName2(s string) string {  return this.name }

Python으로 julian date 산출하기

줄리안 일수 산출을 위해서는 윤년여부 체크를 하여 실제 그 해의 해당 일자에 대한 일수를 365 나 366 일수에 해당 일자로 전환하여 산출한다. 현재 일자를 줄리안 즉 시퀀스 일자대로 수정하는 로직이다 #일자 산출 함수 def dayconvert(month) :      convertDay = 0      dayDict = {'01':0, '02':31, '03':59, '04':90, '05':120,      '06':151, '07':181, '08':212, '09':243, '10':273,      '11':304, '12':334}      convertDay = dayDict[month]      return convertDay #줄리안데이 산출 함수 def julian(date):     year = int(date[0:4])    month = date[4:6]    day = int(date[6:8])    #윤년여부 호출   yuncheck = yunyear(year)   # 일자산출   Dday = dayconvert(month)   # 줄리안 데이 산출   Dday = Dday + day   # 유년여부 체크 및 윤년일 경우 일수조정   if yuncheck == 1:       if int(month) > 2 :            Dday = Dday + 1   # 100일 미만일 경우 포맷조정   if Dday < 100 :          Dday = '0' + str(Dday)   else :         pass   #줄리안 일자를 스트링으로 포맷 조정

Understanding golang Type . 타입이해하기...

Pre-declared Types   go 언어에서 빌드인으로 정의된 데이터 타입들.   string, number(int, uint, float...), boolean 타입들 Named vs Unnamed Type    go언어에서는 사용자 정의(type T_Int int 등)는 기본적으로 named type 으로 인식한다.   그리고  pre-declared types도 named type으로 인식한다.    unnamed type은 composited 타입들로 []int, *int 등을 별도로 인식한다.    named type 들은 메소드 정의가 가능하지만 unnamed type은 메소드를 만들 수 없다.    type Map map[string]string //this is valid func (m Map) Set(key string, value string){ m[key] = value } //this is invalid func (m map[string]string) Set(key string, value string){ m[key] = value } Underlying Type     모든 타입은 underlying type 을 가집니다.  리터럴과 declared type들은 자신의 타입을  underlying type으로 처리한다.     새로 생성되는 타입들에는 기존의 타입들이 있어야 하고 이 타입들도 항상 underlying type 을 가진다.      type Map map[string]string type SpecialMap Map SpecialMap과 Map이 underlying type은 map[string]string 이다. Assignability      //사용자 정의 타입 type Mystring string // pre-de