blob: ce6d5edc130dbbc4571f318ba9accd553197a5f9 [file] [log] [blame] [view]
Jens Geyer0853ab62013-12-17 21:38:44 +01001Thrift Go Software Library
Jens Geyer0e87c462013-06-18 22:25:07 +02002
3License
4=======
5
6Licensed to the Apache Software Foundation (ASF) under one
7or more contributor license agreements. See the NOTICE file
8distributed with this work for additional information
9regarding copyright ownership. The ASF licenses this file
10to you under the Apache License, Version 2.0 (the
11"License"); you may not use this file except in compliance
12with the License. You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16Unless required by applicable law or agreed to in writing,
17software distributed under the License is distributed on an
18"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19KIND, either express or implied. See the License for the
20specific language governing permissions and limitations
21under the License.
22
Jens Geyer0853ab62013-12-17 21:38:44 +010023
Jens Geyer0e87c462013-06-18 22:25:07 +020024Using Thrift with Go
25====================
26
John Boiles57852792018-01-05 14:37:05 -080027Thrift supports Go 1.7+
28
Jens Geyerb7cb9452014-05-17 00:38:06 +020029In following Go conventions, we recommend you use the 'go' tool to install
Jens Geyer0e87c462013-06-18 22:25:07 +020030Thrift for go.
31
D. Can Celasun9ee29512018-10-17 08:44:48 +020032 $ go get github.com/apache/thrift/lib/go/thrift/...
Jens Geyer0e87c462013-06-18 22:25:07 +020033
Jens Geyerb7cb9452014-05-17 00:38:06 +020034Will retrieve and install the most recent version of the package.
Jens Geyer0853ab62013-12-17 21:38:44 +010035
36
37A note about optional fields
38============================
39
40The thrift-to-Go compiler tries to represent thrift IDL structs as Go structs.
41We must be able to distinguish between optional fields that are set to their
42default value and optional values which are actually unset, so the generated
43code represents optional fields via pointers.
44
45This is generally intuitive and works well much of the time, but Go does not
46have a syntax for creating a pointer to a constant in a single expression. That
47is, given a struct like
48
49 struct SomeIDLType {
50 OptionalField *int32
51 }
52
53, the following will not compile:
54
55 x := &SomeIDLType{
56 OptionalField: &(3),
57 }
58
59(Nor is there any other syntax that's built in to the language)
60
61As such, we provide some helpers that do just this under lib/go/thrift/. E.g.,
62
63 x := &SomeIDLType{
64 OptionalField: thrift.Int32Ptr(3),
65 }
66
67And so on. The code generator also creates analogous helpers for user-defined
68typedefs and enums.
Richard Artoulc3a3f652016-07-22 14:26:53 -070069
70Adding custom tags to generated Thrift structs
71==============================================
72
73You can add tags to the auto-generated thrift structs using the following format:
74
75 struct foo {
76 1: required string Bar (go.tag = "some_tag:\"some_tag_value\"")
77 }
78
79which will generate:
80
81 type Foo struct {
82 Bar string `thrift:"bar,1,required" some_tag:"some_tag_value"`
83 }