...

Source file src/github.com/go-kivik/kivik/v4/kivik_example_test.go

Documentation: github.com/go-kivik/kivik/v4

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package kivik_test
    14  
    15  import (
    16  	"fmt"
    17  
    18  	kivik "github.com/go-kivik/kivik/v4"
    19  	_ "github.com/go-kivik/kivik/v4/couchdb" // CouchDB driver, needed for executable example
    20  	"github.com/go-kivik/kivik/v4/driver"
    21  	"github.com/go-kivik/kivik/v4/int/mock"
    22  )
    23  
    24  func init() {
    25  	kivik.Register("driver", &mock.Driver{
    26  		NewClientFunc: func(_ string, _ driver.Options) (driver.Client, error) {
    27  			return &mock.Client{
    28  				DBFunc: func(string, driver.Options) (driver.DB, error) {
    29  					return nil, nil
    30  				},
    31  			}, nil
    32  		},
    33  	})
    34  }
    35  
    36  // New is used to create a client handle. `driver` specifies the name of the
    37  // registered database driver and `dataSourceName` specifies the
    38  // database-specific connection information, such as a URL.
    39  func ExampleNew() {
    40  	client, err := kivik.New("driver", "http://example.com:5984/")
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	fmt.Println("Connected to", client.DSN())
    45  	// Output: Connected to http://example.com:5984/
    46  }
    47  
    48  // With a client handle in hand, you can create a database handle with the DB()
    49  // method to interact with a specific database.
    50  func Example_connecting() {
    51  	client, err := kivik.New("couch", "http://example.com:5984/")
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	db := client.DB("_users")
    56  	fmt.Println("Database handle for " + db.Name())
    57  	// Output: Database handle for _users
    58  }
    59  

View as plain text