"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > GO를 사용하여 Google App Engine Datastore에서 동적 특성을 처리하는 방법은 무엇입니까?

GO를 사용하여 Google App Engine Datastore에서 동적 특성을 처리하는 방법은 무엇입니까?

2025-03-24에 게시되었습니다
검색:179

How to Handle Dynamic Properties in Google App Engine Datastore using Go?

PropertyLoadSaver 인터페이스

속성로드 세이버 인터페이스를 사용하면 엔티티의 속성을 데이터 스토어에로드하고 저장하는 방법을 정의 할 수 있습니다. 이 인터페이스를 구현하면 동적 속성 처리를 제어 할 수 있습니다.

propertylist 유형

Go App 엔진 플랫폼은 PropertyLoadSaver 인터페이스를 구현하는 PropertyList 유형을 제공합니다. PropertyList는 본질적으로 속성 슬라이스로 속성을 동적으로 추가하고 제거 할 수 있습니다.

PropertyList 사용의 예

import "google.golang.org/appengine/datastore"

// Create a PropertyList to hold the dynamic properties.
props := datastore.PropertyList{
    datastore.Property{Name: "time", Value: time.Now()},
    datastore.Property{Name: "email", Value: "[email protected]"},
}

// Create an incomplete key for the new entity.
k := datastore.NewIncompleteKey(ctx, "DynEntity", nil)

// Save the entity using the PropertyList.
key, err := datastore.Put(ctx, k, &props)

PropertyList를 사용하여 동적 속성을 가진 엔티티를 작성하십시오. // 동적 특성을 유지하기 위해 속성 목록을 만듭니다. 소품 : = DataStore.PropertyList { DataStore.property {이름 : "Time", value : time.now ()}, DataStore.property {이름 : "이메일", 값 : "[이메일   보호]"}, } // 새 엔티티에 대한 불완전한 키를 만듭니다. k : = datastore.newincompletekey (CTX, "Dynentity", NIL) // PropertyList를 사용하여 엔티티를 저장합니다. key, err : = datast.put (ctx, k, & props)

이 코드 스 니펫은 "dynentity"종류와 "시간"과 "이메일"이라는 두 가지 동적 특성을 가진 엔티티를 만듭니다. PropertyList는 엔티티의 값으로 저장됩니다.

자신의 PropertyLoadSaver (선택 사항)

import "google.golang.org/appengine/datastore"

type DynEnt map[string]interface{}

func (d *DynEnt) Load(props []datastore.Property) error {
    for _, p := range props {
        (*d)[p.Name] = p.Value
    }
    return nil
}

func (d *DynEnt) Save(props []datastore.Property, err error) error {
    for k, v := range *d {
        props = append(props, datastore.Property{Name: k, Value: v})
    }
    return err
}

import "google.golang.org/appengine/datastore" Dynent Map 유형 [String] 인터페이스 {} func (d *dynent) load (props [] datastore.property) 오류 { _, p : = 범위 소품 { (*d) [p.name] = p.Value } 반환 nil } func (d *dynent) save (props [] datastore.property, err error) 오류 { k, v : = 범위 *d { props = append (props, datastore.property {name : k, value : v}) } 반환 오류 }

import "google.golang.org/appengine/datastore"

// Create a DynEnt with dynamic properties.
d := DynEnt{"email": "[email protected]", "time": time.Now()}

// Create an incomplete key for the new entity.
k := datastore.NewIncompleteKey(ctx, "DynEntity", nil)

// Save the entity using the DynEnt.
key, err := datastore.Put(ctx, k, &d)
최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3