Jens Geyer | 0853ab6 | 2013-12-17 21:38:44 +0100 | [diff] [blame] | 1 | Thrift Go Software Library |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 2 | |
| 3 | License |
| 4 | ======= |
| 5 | |
| 6 | Licensed to the Apache Software Foundation (ASF) under one |
| 7 | or more contributor license agreements. See the NOTICE file |
| 8 | distributed with this work for additional information |
| 9 | regarding copyright ownership. The ASF licenses this file |
| 10 | to you under the Apache License, Version 2.0 (the |
| 11 | "License"); you may not use this file except in compliance |
| 12 | with the License. You may obtain a copy of the License at |
| 13 | |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | Unless required by applicable law or agreed to in writing, |
| 17 | software distributed under the License is distributed on an |
| 18 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 | KIND, either express or implied. See the License for the |
| 20 | specific language governing permissions and limitations |
| 21 | under the License. |
| 22 | |
Jens Geyer | 0853ab6 | 2013-12-17 21:38:44 +0100 | [diff] [blame] | 23 | |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 24 | Using Thrift with Go |
| 25 | ==================== |
| 26 | |
| 27 | In following Go conventions, we reccomend you use the 'go' tool to install |
| 28 | Thrift for go. |
| 29 | |
Jens Geyer | 0853ab6 | 2013-12-17 21:38:44 +0100 | [diff] [blame] | 30 | $ go get git.apache.org/thrift.git/lib/go/thrift |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 31 | |
| 32 | Will install the last stable release. |
Jens Geyer | 0853ab6 | 2013-12-17 21:38:44 +0100 | [diff] [blame] | 33 | |
| 34 | |
| 35 | A note about optional fields |
| 36 | ============================ |
| 37 | |
| 38 | The thrift-to-Go compiler tries to represent thrift IDL structs as Go structs. |
| 39 | We must be able to distinguish between optional fields that are set to their |
| 40 | default value and optional values which are actually unset, so the generated |
| 41 | code represents optional fields via pointers. |
| 42 | |
| 43 | This is generally intuitive and works well much of the time, but Go does not |
| 44 | have a syntax for creating a pointer to a constant in a single expression. That |
| 45 | is, given a struct like |
| 46 | |
| 47 | struct SomeIDLType { |
| 48 | OptionalField *int32 |
| 49 | } |
| 50 | |
| 51 | , the following will not compile: |
| 52 | |
| 53 | x := &SomeIDLType{ |
| 54 | OptionalField: &(3), |
| 55 | } |
| 56 | |
| 57 | (Nor is there any other syntax that's built in to the language) |
| 58 | |
| 59 | As such, we provide some helpers that do just this under lib/go/thrift/. E.g., |
| 60 | |
| 61 | x := &SomeIDLType{ |
| 62 | OptionalField: thrift.Int32Ptr(3), |
| 63 | } |
| 64 | |
| 65 | And so on. The code generator also creates analogous helpers for user-defined |
| 66 | typedefs and enums. |